0

I don't understand how pointers to chars differ from pointers to ints for example:

int a = 10;
int *b = &a;
printf("%d\n", *b); // print 10
char* d = "Hello";
printf("%d\n", (int) *d); // print 72, ASCII of H

Here what is "Hello"? Is every character an address to an int and thus "Hello" an array of addresses?

3
  • 1
    Hello is an array of characters, and d points to the first one. Pointers are just addresses, but the data at that address is to be interpreted as a specific type. Commented Dec 31, 2016 at 16:30
  • 1
    You asked for an integer (%d, inside printf's format string). Commented Dec 31, 2016 at 16:31
  • Use %s for strings. Commented Dec 31, 2016 at 16:31

3 Answers 3

4

C is not strictly type-safe, especially the printf function which accepts anything as input (the ... syntax), how it's interpreted is handled by the format string: %d says "treat the Nth argument as a decimal integer".

A char* is a different type to int*. Another way they differ is how they behave when you increment a char* compared to an int*:

char* c1 = "foo";
char* c2 = c1 + 1;
// c2 is c1 + 1, as sizeof(char) == 1

int* i1 = &123;
int* i2 = i1 + 1;
// i2 is i1 + 4 as sizeof(int) == 4, assuming your system uses 4-byte integers
Sign up to request clarification or add additional context in comments.

5 Comments

But why say char* c1 = "foo" and not char* c1 = &"foo"?
@Ferus Because "foo" is not a char. It's a string which has type char[].
@Code-Apprentice Yes, but it's not an address? Pointers are pointing to addresses I thought.
@Ferus An array can easily be converted into the address of the first element. This is implicit in the assignment.
@Code-Apprentice I see, that's what I didn't understand, thanks.
2

Well if you consider they are all holding addresses ...then

What is the difference?

Difference is suppose I have 10 bytes of memory

---+---+---+---+---+---+---+---+---+---+
|  |   |   |   |   |   |   |   |   |   |  
---+---+---+---+---+---+---+---+---+---+
1    2   3   4   5   6   7   8   9    10

Now suppose a char is of size 1 bye and int of 4 byte.

Now if someone asks you to give the integer starting from address 1 what you will do?

You know that size of int is 4 bytes so you get it and show it.

Similar for char you just get 1 locations value and show it.

Now a pointer similarly needs to know how much it should consider when it is asked to ( dereferenced)...

That's where int* and char* differ. Otherwise both of them hold addresse. And using a char* you can get all byes of an integer but that is overhead on part of user.

Formally....

The pointer to type basically tells the pointer how much it should move in memory from it's current location and how much is bytes staring from it's pointing address needs to be considered when dereferrenced.

Doubt-1 why don't we need &"Hello"?

char *d= "Hello" will place "Hello" in the read-only parts of the memory, and making d a pointer to that makes any writing operation on this memory illegal.

ALSO IN C "hello" has type char[]. And an array can be converted to address of the first element automatically

2 Comments

But for ints you retrieve the address of the variable holding the int, for char you take the char-value "Hello" and not &"Hello", why is that?
But shouldn't it be = char*d = &("Hello"[0]) then? EDIT: What I don't understand is how you can create a pointer and NOT assign it an address (instead assign it "Hello").
0

Firstly, your usage of char* is a bad idea since, whilst the string literal is a 'char[6]', you can't legally modify it, so you would do well to use a const char* instead.

That aside, d will hold the memory address that points to the character H. On most platforms, a char is 1 byte long - thus, d++ will likely increment the memory address by 1 byte.

On the other hand, let's say that an int is 4 bytes. if d was an int* then d++ would increment the memory address by 4.

6 Comments

"Hello" is not const char* but char[6]. It's perfectly valid to assign it to a char*.
You are right, I thought this was a C++, not C question, nonetheless, it is illegal to modify it.
"it is illegal to modify it." - That's true but I don't see how that's particularly relevant to the question.
Obviously it's not, but I firmly believe that when answering questions such as this, it's worthwhile to point out other errors.
"your usage of char* is a bad idea since, whilst the string literal is a 'char[6]', you can't legally modify it," does not apply as code does not even attempting to modify to *d. As coded, there is no error, no UB nor bad idea in using a non-const char * here. The whole const issue is not applicable to the post.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.