I have a hard time understanding pointers in C. can anybody explain me how these operations differ?
int num = 3;
int *ptr;
ptr = #
ptr = num;
ptr = *num;
I just started doing pointers but they seem kind of confusing
I have a hard time understanding pointers in C. can anybody explain me how these operations differ?
int num = 3;
int *ptr;
ptr = #
ptr = num;
ptr = *num;
I just started doing pointers but they seem kind of confusing
int num = 3; // variable declaration
int *ptr; // creating pointer
ptr = # // storing address of num into ptr
now if you dereference ptr you can manipulate num
ex. *ptr=45;
now num=45
ptr = num; // not quite sure about this although it might store the address 3 into ptr
Last edited by camel-man; 10-08-2011 at 10:47 PM.
Make an integer type named num, and assign it the value of 3.Code:int num = 3;
Make a pointer to an integer type called ptr, assign that the value of the address of an integer type.Code:int *ptr; ptr = #That is incorrect, because: =num means "give me the value that num stores", which is an integer, which is not the right type for ptr. The type for a ptr = assignment must be of type int *, not int.Code:ptr = num;That doesn't work either, because num isn't a pointer, so you can't dereference it.Code:ptr = *num;
Quzah.
Hope is the first step on the road to disappointment.
hmm, I'm still kind of confused. The reason why I asked is because I saw those different types of coding on the book I'm using and some other examples from a professor. Maybe I just took the wrong way. Here parts of the codes where I saw them.
Code:int main(void) { char board[3][3] = { {'1','2','3'}, {'4','5','6'}, {'7','8','9'} }; char *pboard = *board; /* A pointer to char */Code:int x = 10; int *p1; p1 = &x;but in this case, ptrx and ptry are both pointers.Code:int x = 10; int y = 5; int *ptrx = &x; int *ptry = &y; *ptrx = *ptrx + 2 ptrx = ptry;
Thanks
First of all you should relax if you are beginner. Pointers is a difficult part of C language. You really want some time and patience to understand their function
Now, you can use printf function in your code in order to print the results ,with this way you will understand better the basic function of pointers.
ptrx and ptry are pointers yes , *ptrx=*ptrx+2 is pointer arithmetic.
I dont know... My compiler hits when I attempt to declare pointers with this way .Code:int *ptrx = &x; int *ptry = &y;
orCode:testtttt.c:6: error: for each function it appears in.)
So, I prefer to use thisCode:testtttt.c:14: warning: assignment makes integer from pointer without a cast
Code:int x=5; int *ptrx; ptrx=&x;
Mr. Lnx
Last edited by Mr.Lnx; 10-09-2011 at 05:26 PM.
No it's not. It's value arithmetic. You are dereferencing pointers there again, so you aren't doing arithmetic on the pointers, but on the value they contain.I think you are getting your errors mixed up somewhere.The only warnings I get when I compile that are to let me know that I didn't actually use q or p for anything.Code:int main( void ) { int x = 0, y = 0; int *p = &x, *q = &y; return 0; }
Quzah.
Hope is the first step on the road to disappointment.
quzah yes you are right.
I confused because I answered fast!!!
*ptrx=*ptrx+2 is NOT pointers arithmetic.
Pointers arithmetic is this => ptrx=ptrx+2
*ptrx is a value arithmetic.
And you are right too for *p=&x;
I have some time to use pointers in my programs so I have forgotten this declaration... I am using only the basic => *p; => 1st step p=&x => 2nd step
Mr. Lnx
Last edited by Mr.Lnx; 10-09-2011 at 05:39 PM.
ptrx=ptry sets ptrx pointing to whatever ptry is pointing to so now if you dereference ptrx it will be value of y which is 5.