Rewrote the file, works. Old code renamed to .old & changed .gitignore to ignore the .vscode folder

This commit is contained in:
marc 2024-10-15 19:19:33 +02:00
parent 4d2785339c
commit f1af8badb1
5 changed files with 221 additions and 164 deletions

View file

@ -1,45 +1,54 @@
#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;
#include <stdio.h>
#include <float.h>
struct PlayStruct {
int int_value;
double double_value;
char a_string[64];
};
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)
{
ps.int_value = 2;
ps.double_value = 3.0;
pps->int_value = 4;
pps->double_value = 5.0;
}
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, "Hello!"};
print_struct(play_struct, &play_struct);
change_struct(play_struct, &play_struct);
//only the struct from the pointer will be changed, the other one doesnt, as it is a local var
print_struct(play_struct, &play_struct);
print_string(play_struct.a_string);
char another_string[16] = "World!";
change_string(play_struct.a_string, another_string);
print_string(play_struct.a_string);
print_string(another_string);
//both will be changed, because they are both pointers
return 0;
}