Why dereferencing operator is not used with a string pointer?

Debojit Acharjee 455 Reputation points
2023-07-24T05:45:53.3766667+00:00

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

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;
}
Developer technologies | C++
Developer technologies | C#
{count} votes

5 answers

Sort by: Most helpful
  1. RLWA32 49,636 Reputation points
    2023-07-24T08:36:00.57+00:00
    *a = 'A';
    

    The above statement stores the character 'A' to the memory address contained in the a char* pointer variable. However, since this variable is not initialized to a valid address before it is used this is a coding error.

    b = "Computer";
    

    This statement stores the address of the string literal to the pointer variable b.If you want to copy the string literal "Computer" to a memory address contained in the char* pointer variable b then you should use the strcpy function.


  2. Bruce (SqlWork.com) 78,006 Reputation points Volunteer Moderator
    2023-07-24T14:50:09.41+00:00

    When you code

    a = b;

    It means copy the value of b to a;

    *a = b;

    It means copy the value of b to the address a points to.

    *a = “string”;

    you are saying copy the value of a string literal, which is the address of the actual char array, to the address a points to.

    0 comments No comments

  3. Barry Schwarz 3,746 Reputation points
    2023-07-24T23:32:22.3166667+00:00

    @Debojit Acharjee Your question is based on a false premise.

    In C, the character, integer, and floating point types are intrinsic types. But in C, string is not a type. It is a concept, namely a contiguous series of char terminated by the char '\0'. While it is pretty common to use the phrase "pointer to string" as convenient linguistic short hand, what you really have is a pointer to char that points to the first character of a series that meets the definition above.

    Therefore, in your code a is a pointer to char but b is not a pointer to string. Dereferencing a, if you ever bothered to set it up properly, would produce the char it points to. Dereferencing b would do the same. However, the %s format is not looking for a char; it is looking for the series of char. By not dereferencing b, you pass its value, the address of what it points to, to printf and then printf can access data at that address and subsequent addresses (courtesy of pointer arithmetic) to deal with each char that is in the string.

    It would behoove you to remember that in C all function arguments are passed by value.

    0 comments No comments

  4. Debojit Acharjee 455 Reputation points
    2023-07-26T13:46:41.37+00:00

    @Barry Schwartz Your explanation is very information but it was not the perfect answer to my question.

    I want to know why the dereference operator (*) was used only with 'a' but not with 'b' in the printf() function while both being pointers.


  5. Bruce (SqlWork.com) 78,006 Reputation points Volunteer Moderator
    2023-07-27T00:52:55.5933333+00:00

    Look at the printf

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

    %c is used to print a char (8 bit int). But a is a pointer to char, not a char, so you need use *a

    %s is used to print the zero terminated char array (or *char). It is expecting pointer to the array. b is a pointer to a string literal (statically allocated string array), so you just pass b.

    again the code (like a lot of your examples)

      *a = 'A';
    

    is coding error, because a was not initialized to a valid value. A decent c compiler would catch this.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.