Some questions about C structures and pointers
by jsbjsb001 from LinuxQuestions.org on (#4R52Y)
As the thread title says; I have some questions about both structures and pointers. While a pointer isn't a structure and vice versa; I figured rather than starting two separate threads, and since one can use pointers with structures, I'll use the one thread instead of two.
I'll start off with my "structure questions" since they seem to feature quite a bit in almost all other code I've tried to read...
But first, I'll start off with what I think I do get about them (feel free to correct me if I'm wrong); a structure is basically a symbolic name for a group of variables that are related to the same "entity", a structure has "member variables". So basically, if I create a structure called "now", and I have a member called "today", without using pointers with it, to access/write it, it would look like now.today
So now on to my question; Does the line below from this code specify three structures called now, target and rel? Because it just isn't clear to me if that's referring to structures or the member variables.
Code:struct timespec now, target, rel;particularly with the following below the above line;
Code:if (clock_gettime(clockid, &now))
return UNSUPPORTED;
target = timespec_add(now, ns);
if (clock_nanosleep(clockid, TIMER_ABSTIME, &target, NULL))
return UNSUPPORTED;
clock_gettime(clockid, &now);I know the & is the address operator, and I know what a return and if statement is, but the above is still confusing to me.
Now on to my "pointer questions"...
Again, I think I know basically what a "pointer" is, in that it's a variable that contains a memory address, being the memory address of what it's being pointed to. Again, feel free to correct me if I'm wrong.
Does the highlighted part of the code below mean "pointer to a char", and if so, is that all it means, in that: it just points to a char? And why would I include that part, as it just isn't making sense to me why that needs to be there.
Code:str = (char *) malloc(15);And yes, I think I know what "type casting" is as well, but I don't get why one would "type cast" a pointer though.
Thanks in advance for any help with this.
James


I'll start off with my "structure questions" since they seem to feature quite a bit in almost all other code I've tried to read...
But first, I'll start off with what I think I do get about them (feel free to correct me if I'm wrong); a structure is basically a symbolic name for a group of variables that are related to the same "entity", a structure has "member variables". So basically, if I create a structure called "now", and I have a member called "today", without using pointers with it, to access/write it, it would look like now.today
So now on to my question; Does the line below from this code specify three structures called now, target and rel? Because it just isn't clear to me if that's referring to structures or the member variables.
Code:struct timespec now, target, rel;particularly with the following below the above line;
Code:if (clock_gettime(clockid, &now))
return UNSUPPORTED;
target = timespec_add(now, ns);
if (clock_nanosleep(clockid, TIMER_ABSTIME, &target, NULL))
return UNSUPPORTED;
clock_gettime(clockid, &now);I know the & is the address operator, and I know what a return and if statement is, but the above is still confusing to me.
Now on to my "pointer questions"...
Again, I think I know basically what a "pointer" is, in that it's a variable that contains a memory address, being the memory address of what it's being pointed to. Again, feel free to correct me if I'm wrong.
Does the highlighted part of the code below mean "pointer to a char", and if so, is that all it means, in that: it just points to a char? And why would I include that part, as it just isn't making sense to me why that needs to be there.
Code:str = (char *) malloc(15);And yes, I think I know what "type casting" is as well, but I don't get why one would "type cast" a pointer though.
Thanks in advance for any help with this.
James