# Thread: int & char pointers

1. ## int & char pointers

Why is the following code valid for a char pointer but not for a int pointer?
I am confused as to why I cant use the same approach for both.

Code:
```char *y;

y = “This is great”;

printf(“&#37;s”, y);```
But this will fail
Code:
```int *x;

x = 25;

printf(“%d”, x);```

2. %s expects a pointer to char
%d expects an int

3. Because %s expects a char pointer, which is what y is.
%d expects an int.

int x = 42;
int *px = &x;
*px = 21;
printf( "%d", *px );

Dereferencing a pointer to an int gets an int, which is what %d needs.

4. ## Great explanation Poseidon - God of the C

I was really messed up on that but it makes more sense now. This learn C in 21 days book is not all that detailed. Anyways I can see now the difference lies in %s and %d so thanks.

5. ## I still have one more lingering thought which is never good

So both of these are legal ways to assign values to a pointer?

y = “This is great”;

x = 25;

and I just was wrong in the manner I tried to use printf as you stated?

6. No, but the first one is fine.

If x is defined as an int *, then you are assigning an address of 25 to x, not the value of 25.

If you want to point to the address of the 25, then do this:

int * x ;
int n = 25 ;
x = &n ;

Now, x point to the address of an int.

7. ## So int and char pointers do not work the same at all right?

Is the below true then?

p is the pointer
sorry bout the uppercase

WHEN USING CHAR POINTERS

P = ENTIRE STRING

&P = ADDRESS OF THE POINTER

*P = 1RST BLOCK OF THE STRING

*(P + 2) = 3RD BLOCK OF THE STRING

WHEN USING INT POINTERS

P = ADDRESS OF THE CONTENTS P IS POINTING TO

*P = CONTENTS OF THE POINTER (1 BLOCK OF MEMORY)

THE POINTER MUST BE INITIALIZED AND VALUES ASSIGNED
IN A MANNER SUCH AS BELOW:

int time = 500;

int *carl2;

carl2 = &time;

8. char and int pointer are very close - they just differ in "block" size
for char pointer it is sizeof(char) = 1 byte
for int pointer it is sizeof(int)

as additional benefit sequence of chars with nul-terminating char is processed as a C-string by standard library functions desined to process the C-strings

there is no such assumption for sequense of ints...