Advertisement

Pointer questions

Started by January 16, 2003 02:03 PM
1 comment, last by zackriggle 21 years, 10 months ago
Alright, I am extremely (and have been for a while) confused about pointers. I think I have the gist of them, but I always end up with an invalid pointer or the like. Question/Example 1: char ch[255]; strcpy(ch,"4566\0"); char *pch; pch = ch; // Is this making pch point to ch? int a; a = atoi(*pch); // Do I do this or: // a = atoi(pch); Question/Example 2: int num[3]; num = 5,25,13; int *pnum; pnum = num; // pnum = num[0], right? pnum++; // pnum = num[1] now? int *p2; p2 = &pnum // does p2 = #[1] or p2 = &pnum?
Firstly,

you use

a = atoi(pch);

as pch is a pointer whereas using

a = atoi(*pch);

passing the contents at the address pointed to by pch to the function which in this case would be 4.

Secondly

p2 = &pnum, ie the address in memory where pnum is stored. Therefore *p2 = #[ 1 ], or the contents at the address pointed to by p2 is the address of the second element in the array num.

===========================
There are 10 types of people in the world. Those that understand binary and those that don''t.

( My views in no way reflect the views of my employer. )
===========================There are 10 types of people in the world. Those that understand binary and those that don't.( My views in no way reflect the views of my employer. )
Advertisement
You must declare p2 as int **p2 (pointer to a pointer to an int). Otherwise, the code in question 2 won't compile, disregarding the syntax error in the initialization of your array.

quote: Original post by Gav
passing the contents at the address pointed to by pch to the function which in this case would be 4.


The contents at the address pointed to by pch is '4', not 4. Sorry for being pedantic, but there is a big difference.

[edited by - micepick on January 16, 2003 3:21:08 PM]

This topic is closed to new replies.

Advertisement