Question : is the answer related to the buffer or to pointers...

B

bpascal123

Hi,

In the code below, i'd like to understand why ptr_c doesn't return
something as a value whereas ptr_x returns a number. I understand
ptr_c is of character type

#include <stdio.h>

int main(void)
{
char c, *ptr_c ;
int x, *ptr_x ;

c = 'A' ;
x = 123 ;

printf("\n\nVariable c :\naddress = 0x%p\nvalue = %c\n", &c, c) ;
ptr_c = &c ;
printf("\nPointer ptr_c :\naddress = 0x%p\nvalue = %c\n",
&ptr_c, ptr_c) ;
printf("*ptr_c => %c\n\n\n", *ptr_c) ;

printf("\nVariable x :\naddress = 0x%p\nvalue = %d\n", &x, x) ;
ptr_x = &x ;
printf("\nPointer ptr_x :\naddress = 0x%p\nvalue = %d\n\n",
&ptr_x, ptr_x) ;
printf("*ptr_x => %d\n\n\n", *ptr_x) ;

return 0 ;
}

Is it because i'm simply calling an empty variable value in the
buffer ?
Is this related to the buffer?

Thx,

Pascal
 
K

Keith Thompson

In the code below, i'd like to understand why ptr_c doesn't return
something as a value whereas ptr_x returns a number.

ptr_c and ptr_x are variables (more precisely "objects"). They don't
return anything. A variable *has* a value. (A function *returns* a
value, and an expression *yields* a value.)
I understand
ptr_c is of character type

Then you understand incorrectly. ptr_c is of pointer type,
specifically pointer to char or "char*". c is of character type
(char).

In general, whenever you use the "%p" format, you should explicitly
convert (i.e., cast) the corresponding argument to void*. Also,
preceding the "%p" with "0x" is not necessary. On many systems, the
format produced by "%p" already includes the "0x"; on others, the
format isn't even hexadecimal. Let printf decide how to print
pointers for you.

I find that the multiple '\n's make both the code and the output
difficult to read.
#include <stdio.h>

int main(void)
{
char c, *ptr_c ;
int x, *ptr_x ;

c = 'A' ;
x = 123 ;

printf("\n\nVariable c :\naddress = 0x%p\nvalue = %c\n", &c, c) ;
ptr_c = &c ;
printf("\nPointer ptr_c :\naddress = 0x%p\nvalue = %c\n",
&ptr_c, ptr_c) ;

ptr_c is of type char*. You try to print it with a "%c" format, which
expects an int (typically a character value).
printf("*ptr_c => %c\n\n\n", *ptr_c) ;

printf("\nVariable x :\naddress = 0x%p\nvalue = %d\n", &x, x) ;
ptr_x = &x ;
printf("\nPointer ptr_x :\naddress = 0x%p\nvalue = %d\n\n",
&ptr_x, ptr_x) ;

Again, ptr_x is of type int*, and you try to print it with "%d", which
requires an int argument.
printf("*ptr_x => %d\n\n\n", *ptr_x) ;

return 0 ;
}

Is it because i'm simply calling an empty variable value in the
buffer ?
Is this related to the buffer?

No. What buffer are you talking about? What "empty variable value"?

Here's a modified version of your program:

#include <stdio.h>

int main(void)
{
char c, *ptr_c;
int x, *ptr_x;

c = 'A';
x = 123;

printf("c: address = %p, value = '%c'\n", (void*)&c, c);

ptr_c = &c;
printf("ptr_c: address = %p, value = %p, pointed-to value = '%c'\n",
(void*)&ptr_c, (void*)ptr_c, *ptr_c);

printf("x: address = %p, value = %d\n", (void*)&x, x);

ptr_x = &x;
printf("ptr_x: address = %p, value = %p, pointed-to value = %d\n",
(void*)&ptr_x, (void*)ptr_x, *ptr_x);

return 0;
}

and its output on my system:

c: address = 0xbfa3b163, value = 'A'
ptr_c: address = 0xbfa3b15c, value = 0xbfa3b163, pointed-to value = 'A'
x: address = 0xbfa3b158, value = 123
ptr_x: address = 0xbfa3b154, value = 0xbfa3b158, pointed-to value = 123
 
F

Flash Gordon

Hi,

In the code below, i'd like to understand why ptr_c doesn't return
something as a value whereas ptr_x returns a number. I understand
ptr_c is of character type

#include <stdio.h>

int main(void)
{
char c, *ptr_c ;
int x, *ptr_x ;

<snip>

Stop right there and think again about what types ptr_c and ptr_x are.
They are *not* the same as c and x! That seems to be where you have
fallen down.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top