segmentation fault

This commit is contained in:
MarcUs7i 2024-10-10 17:00:19 +02:00
parent 138892d264
commit 4d2785339c
2 changed files with 45 additions and 0 deletions

BIN
pointer_fun_again Normal file

Binary file not shown.

45
pointer_fun_again.c Normal file
View file

@ -0,0 +1,45 @@
#include <stdio.h>
#include <float.h>
#include <string.h>
struct PlayStruct {
int int_value;
double double_value;
char* a_string;
};
void print_struct(struct PlayStruct ps, struct PlayStruct* pps) {
printf("Values of struct ps: %d, %lf, %s\n", ps.int_value, ps.double_value, ps.a_string);
printf("Values of struct pps: %d, %lf, %s\n", pps->int_value, pps->double_value, pps->a_string);
}
void change_struct(struct PlayStruct ps, struct PlayStruct* pps) {
struct PlayStruct new_ps = {ps.int_value + 1, ps.double_value * 2, ps.a_string};
*pps = new_ps;
}
void print_string(char* string_to_print) {
printf("%s\n", string_to_print);
}
void change_string(char* string1, char** p_string) {
string1[2] = '\0';
*p_string[1] = '\0';
}
int main(int argc, char* argv[]) {
struct PlayStruct play_struct = {1, 2.0, "3456"};
struct PlayStruct* play_struct_pointer = &play_struct;
print_struct(play_struct, play_struct_pointer);
change_struct(play_struct, play_struct_pointer);
//first value gets incremented by 1 & second value gets multiplied by 2 & the string wont be changed
print_struct(play_struct, play_struct_pointer);
print_string(play_struct.a_string);
char* string2 = "Test";
change_string(play_struct.a_string, &string2);
print_string(string2);
return 0;
}