The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
basic question about pointers in C
Discuss basic question about pointers in C in the C Programming forum on Dev Shed. basic question about pointers in C C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 4th, 2003, 08:04 PM
|
|
matthewdoucette.com
|
|
Join Date: May 2002
Posts: 635

Time spent in forums: 10 h 8 m 16 sec
Reputation Power: 11
|
|
|
basic question about pointers in C
(I want to know some extremely basic facts about pointers in C, but first of all, I want to let you know that I understand what pointers are. Their value, or what they hold, is an address to a piece of data or a variable.)
What should a pointer's type be defined as? Is an address in C correlated to the type of data stored at that address? In other words, does a pointer's type have to equal the type stored at the address it is pointing to upon declaration?
For example, if I want a pointer to a char and a long int, is this how I should do it?
// regular variables
char c;
long int i;
// pointers
char *cp
long int *ip;
What confuses me is that if this is true, I just created two pointers with the first being a byte in size, and the second being four bytes in size. What am I misunderstanding? Does declaring a pointer using "char" or "long int" not effect the actual size of the pointer itself, but instead 'tells' the program that those pointers are pointing to variables of that size instead? If so, what confuses me with that is how can I have a pointer to char point to a full null-terminated string that is bigger than a char?
Sorry, I am just a bit rusty with programming C after progrmming all this perl code...
__________________
Matthew Doucette / Xona.com
Last edited by Doucette : February 4th, 2003 at 08:14 PM.
|

February 4th, 2003, 08:32 PM
|
|
Contributing User
|
|
Join Date: Nov 2002
Location: Blacksburg VA/Philly PA
Posts: 38
Time spent in forums: 2 h 29 m
Reputation Power: 11
|
|
|
pointers are trickier than they should be. when you declare something such as:
double Target = 10;
double* tPtr = Target;
you first declare a double Target which takes up 8 bits of memory (i believe don't kill me if i am wrong), and then next you declare a pointer; tPtr which points to Target. the act of pointing stores the memory address of Target into tPtr. the memory allocated for the storage of memory addresses is 4 bits (i believe as well).
no matter what target you are pointing to, a memory address will be stored in the pointer.
|

February 4th, 2003, 08:42 PM
|
|
matthewdoucette.com
|
|
Join Date: May 2002
Posts: 635

Time spent in forums: 10 h 8 m 16 sec
Reputation Power: 11
|
|
|
Thanks pschmerg, I am fairly certian that the actual size of the pointer itself stays the same size too. Let me ask directly where I am stuck with pointers:
I have this declared...
char *data;
...and it points to a string. The pointer is derived from getenv(), incidentally. Anyway, this prints the string...
printf("%s",data);
...but how do I extract each individual character out of the string? This is where I am lost.
|

February 4th, 2003, 10:41 PM
|
|
Offensive Member
|
|
Join Date: Oct 2002
Location: in the perfect world
|
|
|
char *pString=NULL;
//fill pString with data
while( *pString!='\0' && *pString!=',' )
{
*pString++;
}
__________________
The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.
Frank Zappa
|

February 4th, 2003, 11:07 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
Re: basic question about pointers in C
Quote: Originally posted by Doucette
Does declaring a pointer using "char" or "long int" not effect the actual size of the pointer itself, but instead 'tells' the program that those pointers are pointing to variables of that size instead? If so, what confuses me with that is how can I have a pointer to char point to a full null-terminated string that is bigger than a char? |
Yep, right on the mark there. Not only that, it also tells the compiler by how many bytes to increment the pointer, when you increase the pointer by 1.
For example:
Code:
char ch[5] = { 'a', 'b', 'c', 'd', 'e'};
double dbl[5] = { 1, 2, 3, 4, 5};
char *pChar;
double *pDouble;
/* Assign the pointers to the array */
pChar = ch; /* or &ch[0], if you like */
pDouble = dbl; /* or &dbl[0] if you like */
/* Print the pointers and the values they point to */
printf("Addresses: pChar = %d, pDouble = %d\n", pChar, pDouble);
printf("Value of *pChar = %c, *pDouble = %lf\n", *pChar, *pDouble);
pChar++; /* Point to the next element in the array */
pDouble++; /* Point to the next element in the array */
/* You can also do pDouble = pDouble + 1 or whatever */
/* Print the pointers and the values they point to */
printf("Addresses: pChar = %d, pDouble = %d\n", pChar, pDouble);
printf("Value of *pChar = %c, *pDouble = %lf\n", *pChar, *pDouble);
In the above code, we assign two 5 element arrays and assign two pointers to point to the first element of each array. Then we print out the addresses that the pointers point to, and the values in the addresses.
Now comes the big kicker --- Note that we increment both pointers by 1. Now we print the new addresses that the pointers point to, as well as the values at the addresses. If you're compiling this on a 32 bit compiler, you might notice that pChar goes up by 1 byte, but pDouble goes up by 8 bytes! The reason for this is that the compiler knows that pDouble is a pointer to a double and hence it increases the value of the address by the size of a double (i.e.) 8 bytes.
Hope this helps! 
|

February 5th, 2003, 02:59 PM
|
|
.
|
|
Join Date: Dec 2002
Posts: 296
Time spent in forums: < 1 sec
Reputation Power: 11
|
|
Quote: | declaring a pointer using "char" or "long int" not effect the actual size of the pointer itself, but instead 'tells' the program that those pointers are pointing to variables of that size instead |
so you could look at pointers as another variable type (like char or int etc) i suppose. they've got a particular storage capacity associated with them. but also have a particular use.
i've read a few times that in general using pointers is much more efficient than not. this efficiency must be referring to speed right? cause they're certainly not smaller. easily twice the size of what it's pointing to, and easily more than twice. at first glance efficiency isn't the first thing that springs to mind.
i guess speed is more important than size normally, so if that's the case, it's a good idea to use pointers and that's where the efficiency description comes from? if size is your main concern then not using pointers would be a good idea?
|

February 5th, 2003, 03:44 PM
|
|
matthewdoucette.com
|
|
Join Date: May 2002
Posts: 635

Time spent in forums: 10 h 8 m 16 sec
Reputation Power: 11
|
|
|
I am concerned only with optimizations that increase speed, not shrink size. I think size is normally not an issue.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|