9 Replies - 1626 Views - Last Post: 03 March 2010 - 01:33 PM Rate Topic: ****- 1 Votes

#1 konos5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 09-September 09

C char pointer

Posted 28 February 2010 - 11:28 AM

Hi everyone,
I am new to C and I have this very simple code:

#include <stdio.h>
char *name;

int main (void){
	 name = "Hello";

printf("name has the value %p and points to %d and is stored at %p\n", name, *name, (void *)&name);

name = "Josh";

printf("name has the value %p and points to %p and is stored at %p\n", name, *name, (void *)&name);

return 0;
}



Can someone explain to me the exact differences of name, &name and *name?
What I know so far is that &name contains the memory address of the pointer itself.
But what about name and *name?
*name SHOULD contain the memory address of the place it points to and name should contain the same as well...
But if I compile and run it I get different "numbers"..What exactly is happening?

Thank you in advance

Is This A Good Question/Topic? 0
  • +

Replies To: C char pointer

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: C char pointer

Posted 28 February 2010 - 12:08 PM

*
POPULAR

Usually this is cut and dry with most primitive pointer types, but with char, there's a little "fuzziness" because it's how C represents strings.


A piece at a time [I'm going to use C++ style comments for easier readability]:

char* name; //declares a pointer whose space is allocated on the stack (4 bytes usually)
//at this point it points to nothing.


name = "Hello"; //the pointer now points to stack memory (i.e. not dynamic/heap) "Hello" usually done at compile time
//another way to look at this is

name[0] = 'H'
name[1] = 'e'
//etc...

// printf()
// %p is pointer address
// so 'name' here prints the address of what the pointer is holding, 
// if you wanted the actual address of the pointer you'd need to do &name 
// when you do *name it returns the contents of the pointer  [dereferencing]
// more specifically it gives the first index of the array name[0], which is the "base" of the pointer 
// because you told printf to display an integer it prints 72 which is the ASCII value of 'h' which is in the first index 



//here's a cleaner example using integers
int x = 5;
int* ptr = &x;

//notice that the last two will be the same ptr hold's x's address
printf("ptr's address: %p It points to 'x' at %p Value held in ptr: %p\n\n", &ptr, &x, ptr);



Taking your example:

char *name;

int main (void){
	name = "Hello";
	printf("name has the value %p and points to %s and is stored at %p\n", name, name, &name); //changed the 2nd parameter
	name = "Josh";
	printf("name has the value %p and points to %s and is stored at %p\n", name, name, (void *)&name);
	return 0;
}



First printf(): it prints the address held by name, the contents of name ("Hello"), and the actual address of name
Second printf(): It prints the address held by name (now different because we changed what it pointed to), contents ("Josh"), and the address of name itself which has not changed.


Example output on my machine:

Quote

name has the value 0042EC70 and points to Hello and is stored at 004333A4
name has the value 0042EC20 and points to Josh and is stored at 004333A4



More on this can be found here
Was This Post Helpful? 5
  • +
  • -

#3 konos5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 09-September 09

Re: C char pointer

Posted 01 March 2010 - 10:15 PM

Thanks a lot for your answer. You couldn't have been clearer.
But there is still one thing I can't understand.

When saying:
printf("name has the value %p and points to %s and is stored at %p\n", name, name, (void *)&name);


I have a small objection about the second argument. Here 'name' contains the address of where the pointer actually points to and this is printed out initially with our first formatting argument(using %p). But at the second argument where we format with %s, how can the compiler print out the contents of that address? (that is "Hello")

From what I know so far, only by dereferencing we can retrieve the actual value of a "place" in memory.
Therefore, if 'name' holds a memory address(that is the address of 'H'), and we don't dereference it, how come we get a value instead of an address and furthermore how do we get the whole array of characters back??

Thanks again.
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: C char pointer

Posted 02 March 2010 - 08:14 AM

Most printf arguments require a pointer, so all we need to do is pass name by itself. Remember that name actually starts at name[0] and goes until the null terminating operator '\0' is hit. Since we told it to expect a string [%s] printf starts at the first index and goes until '\0' [Since we pointed our pointer to a stack allocated string, the null terminator is implicit.]
Was This Post Helpful? 0
  • +
  • -

#5 konos5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 09-September 09

Re: C char pointer

Posted 02 March 2010 - 10:12 AM

From your last post,you make me think that:

printf("name has the value %s" , name)


is almost the same as:

for (i = 0; i<name.length; i++) //.length is propably wrong I just put it in here to demostrate my idea
printf("name has the value name[%c]", name[i]);


Is the above idea correct?

Cause if it is so, then the format argument [%s] must have some "super" functionality as it takes a memory address and automatically not only looks up and prints its value, but also moves on to the next consecutive memory cells retrieving their values until it hits the null terminating operator '\0'.

Something's wrong here... too powerful for a simple format argument...

This post has been edited by konos5: 02 March 2010 - 10:15 AM

Was This Post Helpful? 0
  • +
  • -

#6 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: C char pointer

Posted 02 March 2010 - 10:26 AM

You are absolutely correct in what you say. printf with %s is a powerful facility which we as programmers seem to take for granted. It is exactly as you described it in the code using the %c. What you have coded is the type of code which printf uses internally when it deals with a %s (actually it's not quite but for you, it is enough to know).
Was This Post Helpful? 1
  • +
  • -

#7 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: C char pointer

Posted 02 March 2010 - 10:27 AM

for (i = 0; i<name.length; i++)


I think you meant strlen(name).

"Cause if it is so, then the format argument [%s] must have some "super" functionality as it takes a memory address and automatically not only looks up and prints its value, but also moves on to the next consecutive memory cells retrieving their values until it hits the null terminating operator '\0'."
Sort of. %s stands for "string format", so, you use it when you want to print an array of characters. %c stands for "character format", and you use it when you want to print ONLY one character.
Now, this method:
printf("name has the value %s" , name)


this printing method tells the compiler to print the string that is found at the memory address of the parameter that is passed in (in your case, "name"). So, this prints the data of "name". "%s" format is specially designed for that, it's able to print the entire data from a memorry address.
I call the second method printing char by char. It's the same thing as the first method, but you just print the data of "name", char-by-char (should be avoided)

This post has been edited by sarmanu: 02 March 2010 - 10:32 AM

Was This Post Helpful? 1
  • +
  • -

#8 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: C char pointer

Posted 02 March 2010 - 10:44 AM

Yes, that's correct.Nothings wrong here.Character arrays are unique.So printf with a %s treats them differently.
Was This Post Helpful? 0
  • +
  • -

#9 konos5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 09-September 09

Re: C char pointer

Posted 03 March 2010 - 01:07 PM

This is getting really interesting...

So in other words you are telling me that if FOR EXAMPLE, the word 'Hello' is stored at memory address number 0022FF24 (pointed by 'name'), by using the format %s, the compiler will also look at 0022FF25, 0022FF26, 0022FF27, 0022FF28 (to retrieve the rest of the characters) until it hits the null terminator which will be found on 0022FF29 in that case.

If the above scenario is correct, then that could create some serious problems in cause the programmer somehow later on corrupts the data in locations 0022FF26, 0022FF27, 0022FF28...
It could create a program crash for example...

This post has been edited by konos5: 03 March 2010 - 01:08 PM

Was This Post Helpful? 0
  • +
  • -

#10 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: C char pointer

Posted 03 March 2010 - 01:33 PM

Yes, with power comes responsibility.
Therefore you must ensure that C-style strings are null terminated.If it isnt, then your program may act wierd.
C++ strings are more safe to use.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1