C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 4th, 2003, 08:04 PM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
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.

Reply With Quote
  #2  
Old February 4th, 2003, 08:32 PM
pschmerg pschmerg is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: Blacksburg VA/Philly PA
Posts: 38 pschmerg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 29 m
Reputation Power: 11
Send a message via AIM to pschmerg
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.

Reply With Quote
  #3  
Old February 4th, 2003, 08:42 PM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
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.

Reply With Quote
  #4  
Old February 4th, 2003, 10:41 PM
TechNoFear TechNoFear is offline
Offensive Member
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2002
Location: in the perfect world
Posts: 618 TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 13 h 50 m
Reputation Power: 26
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

Reply With Quote
  #5  
Old February 4th, 2003, 11:07 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,385 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 30 m 25 sec
Reputation Power: 4080
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!

Reply With Quote
  #6  
Old February 5th, 2003, 02:59 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #7  
Old February 5th, 2003, 03:44 PM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > basic question about pointers in C

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap