-4

Why the indirection/dereferencing operator (*) is used with a pointer of any data type except a string? 'char'

In the following program the letter 'A' and the string "Computer" are stored using the character pointers but only for the pointer 'a' the dereferencing operator (*) is used. Why is that?

#include <stdio.h>

int main()
{
  char *a, *b;

  *a = 'A';
  b = "Computer";

  printf("%c %s", *a, b);

  return 0;
}

Why is the * operator not needed while assigning a string to a pointer?

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • 1
    Both won't even compile in c++. – πάντα ῥεῖ Jul 24 '23 at 10:53
  • 2
    `*a = 'A'` is undefined behaviour, there is no memory allocated to store the value. – Quimby Jul 24 '23 at 10:53
  • 1
    `*a = 'A';` is invalid. The pointer `a` haven't been initialized, it doesn't point anywhere valid, and dereferencing it will lead to *undefined behavior*. – Some programmer dude Jul 24 '23 at 10:54
  • Also note that for any pointer to array `a` and index `i`, the expression `a[i]` is *exactly* equal to `*(a + i)`. That means when you have `a[0]` then that's equal to `*(a + 0)` which is equal to `*a`. So `*a = 'A'` is the same as `a[0] = 'A'`. – Some programmer dude Jul 24 '23 at 10:55
  • As for `b = "Computer"` you have to remember that literal strings are actually *arrays* of characters (including the string null-terminator), stored somewhere in memory. The assignment you have will make `b` point to the first character in the array holding `"Computer"`. – Some programmer dude Jul 24 '23 at 10:56
  • 2
    Finally, please note that the semantics for literal strings is *different* between C and C++. As C++ source, your program is invalid. That's because in C++ literal strings are *constant*. You must have a `const char*` pointer for them. In C literal strings are just not allowed to be modified, they are in essence read-only, but not constant. – Some programmer dude Jul 24 '23 at 10:57
  • 1
    Related: [What is array to pointer decay?](https://stackoverflow.com/q/1461432/12149471) – Andreas Wenzel Jul 24 '23 at 10:59
  • Your point would be valid if your code was `string* b; b = "abc";` but it isn't, it's `char* b;` and `char` is not a string, it's a single character. In C there is no string data type, instead strings are simulated with char pointers. That's why no `*` is required in `b = "abc";` – john Jul 24 '23 at 13:57
  • Please do not deface your question or otherwise remove its meaning. Others have spent their time answering it and removing the meaning from your question is a disservice to everyone who's put effort into helping you. – Andrew Henle Jul 26 '23 at 16:00
  • You need different things because %s is different than %c. – ikegami Jul 26 '23 at 17:01

4 Answers4

2

Because b = "Computer"; does not copy the string. "Computer" is called "string literal" and it is a char array.

You assign pointer b with reference to the first string literal character.

It is an equivalent of:

    char *b;
    const char literal[] = {'C', 'o', 'm', 'p', 'u', 't', 'e', 'r', 0};
    b = &literal[0];

0___________
  • 60,014
  • 4
  • 34
  • 74
2

Why the indirection/dereferencing operator (*) is used with a pointer of any data type except a string?

The question is moot, because its premise is incorrect. The dereferencing operator is applied to a pointer when one wants to refer to the object to which the pointer points. It is omitted to refer to the pointer itself. Both alternatives are used with pointers of every type.

Additionally, in C, "string" is not a data type. It is a description of a (part of a) value that arrays of char can hold: a sequence of one or more chars, the last having value zero, and all others being nonzero.

In the following program the letter 'A' and the string "Computer" are stored using the character pointers but only for the pointer 'a' the dereferencing operator (*) is used. Why is that?

Again, moot because the premise is incorrect. This ...

    *a = 'A';

... attempts to store the value 'A' (an int in C; a char in C++) in the char object to which pointer a points. If a actually pointed to a char then the result would be to set the value of that pointed-to char, but a does not point to anything, having never been assigned a valid pointer value, so the behavior is undefined.

On the other hand, this ...

  b = "Computer";

... assigns a (pointer) value to b itself. In this case, that value points to the first char of an unmodifiable static array of char containing the chars expressed by the string literal, including a string terminator. This is a consequence of the language's definition of string literals (as representing arrays) and of the standard rules for the behavior of arrays where they appear in expressions.

Why is the * operator not needed while assigning a string to a pointer?

Because you never need or want * to assign to (or read from) an lvalue designating a pointer. Its use is rather to access the object to which the value of a pointer points. Remember always that these are different things, with separate identity and storage (when they have identity and storage at all).

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
0

To answer your question, we need to get rid of some syntactic sugar first and know exactly what b is:

  char *b;
  b = "Computer";

is (almost) equivalent to

  const char *b;
  const char str[] = {'C', 'o', 'm', 'p', 'u', 't', 'e', 'r', '\0'};
  b = &str[0];

Meaning b is a pointer to the first element in an array of char elements. Or simpler, b just points to a char

printf("%c", *b) expects a char (%c), and by using *b we are giving printf the first char from the string (or char array) "Computer"

printf("%s", b) expects a pointer (%s). We are thus providing b which points to "Computer". Why? Because under the hood, printf starts at a location, reads a character and goes to the next location (which is b + 1). It does that until printf reads the value 0 somewhere along its path.

So the core idea is that you're indeed dealing with a pointer, but printf needs itself a pointer to go through an array of char

Note that the charatcer '0' is not the number 0, but the number 0 is equal to the character '\0' which is what you sometimes see in char arrays like in my example.

As an extra on why the above snippets are not exactly the same: A string in code is stored in a read-only location, whereas the assignment as an array of characters is stored in modifiable memory. The const keyword ensures immutability, but both of these strings are still stored in entirely different locations and thus behaviour might not be the same.

Oliver
  • 16
  • 2
0

Whenever you use the char* data type and assign a string to it, you are actually making a pointer to an array of characters, but whenever you assign a single character to it, you are making a pointer to a single character for example:

    char Var1 = 'A';
    char Var2[9] = {'C','o','m','p','u','t','e','r','\n'};
    
    char* a = &Var1;
    char* b = Var2;
    printf("%c %s\n",*a,c);

does (about) the same thing as

char *a = malloc(1);
  char *b = malloc(8);

  *a = 'A';
  b = "Computer";

  printf("%c %s\n", *a, b);
  free(a);
  free(b);


(please take note that the code you originally provided does not function on its own and I had to change it a bit) I hope this helps you understand the char pointers data type a better

user17231236
  • 13
  • 1
  • 3