I started learning structures and I've some problems(see below)which I don't know why when I compile the code, it either doesn't work at all and clearing out an error, or the code work but there's a misunderstanding in the code's script.
P.S: I was having another thread related to structures but I can't ask in the same thread more than one issue!
problem 1:
when I want to print a simple code for printing a char by using a struct, but the result is something like "?" not a char at all!
Code:
#include<stdio.h>
#include<stdlib.h>
struct Test
{
char x;
};
int main(void)
{
struct Test serTest;
serTest.x ="f"; // the debugger shows there's an error here, idk why!! //
printf("x=%c\n",serTest.x); // it should print here the char value that's stored into serTest..//
return 0;
}
Problem 2:
while I was collecting information about structures there was accidently an example which is shown below, the case isn't in the code itself (not in the script itself)....the issue is, in implementing the notes that I wrote them in "//" (in red color)near every code I would get a holy ........ bugs!; I've colored the notes in Red color for telling me please why I can't implement what I've written in the notes.
Code:
#include<stdio.h>
struct Test
{
int x;
};
void updateStruct(struct Test *y); // here, why I can't for example write instead of the current function, a function "updateStruct(struct Test y)" without using a pointer itself? I tried to remove "*" but it says there's error, so why can't?//
int main(void)
{
struct Test sTest;
sTest.x = 2;
printf("x = %d\n",sTest.x);
updateStruct(&sTest); // and here why I cant write instead of &sTest, an equivalent one as a sTest.x? or actually as a sTest (without &)?//
//what does even the operator "&" near sTest in updateStruct function stand for? can't get ride of it? //
printf("x = %d\n",sTest.x);
return 0;
}
void updateStruct(struct Test *y) // here why I actually need a pointer in struct at all? I can use the general way just write " y " without "*" (meaning without the pointer)//
{
(*y).x = 3; // line of interest
}
If you explain properly what I've asked in upper including the notes, I would appreciate your assistance and I will be wholly known of the structures absolutely!
thanks alot!!