Thread: Why does a char pointer store a string directly while a pointer stores a memory addr

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    92

    Why does a char pointer store a string directly while a pointer stores a memory addr

    In C programming, pointers is variable that hold memory addresses of another variable. However, the way pointers work with different data types can be confusing for me

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    int main() {
        // Assigning a string to the pointer 'y'
        char *y = "abcd"; // confued in this line
        printf("%s\n", y); // Output: "abcd"
    
    
        // Assigning a value of 10 to the pointer 'x'
        int num = 10; // Define an integer variable 'num'
        int *x = &num; // 'x' points to the address of the integer variable 'num'
        printf("%d\n", *x); // Output: 10
    
    
        return 0;
    
    }


    I understand that a string in C is an array of characters,I'm curious to know the underlying reason and the logic behind this distinction. If anyone could shed some light on this topic, I'd greatly appreciate it
    Last edited by Kittu20; 07-26-2023 at 02:38 AM.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    // Assigning a string to the pointer 'y'
    char *y = "abcd"; // confued in this line
    printf("%s\n", y); // Output: "abcd"
    y is a pointer to one character. Doesn't matter is it points to a char variable, an array of chars, or a constant string, as in this case.
    "abcd" is a constant Nul terminated array of type char.
    y points to one char, the first character in the constant string.
    The "%s" in the printf() expects the address of a Nul terminated array of type char, in this case, y, a pointer to the first char in the string, and displays all the characters up to the terminating Null byte.

    An up to date book on the C Programming language would explain this and all details of the language. Studying the whole book and doing all the exercises in each chapter, would prepare you to program in C. I have listed my recommendations many times in this forum.

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    what happens if you do printf("%p\n", y) then printf("%p\n", y[1]) are the results different?

    i think the answer is that strings are effectively arrays so each character is stored at a separate address where as in your example *x points to a single address with the entire integer stored at that address.

    a char takes one byte so it cant hold a whole string hence as you said each character is stored in separate memory addresses

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    Quote Originally Posted by rstanley View Post
    y is a pointer to one character. Doesn't matter is it points to a char variable, an array of chars, or a constant string, as in this case.
    "abcd" is a constant Nul terminated array of type char.
    y points to one char, the first character in the constant string.
    The "%s" in the printf() expects the address of a Nul terminated array of type char, in this case, y, a pointer to the first char in the string, and displays all the characters up to the terminating Null byte.

    An up to date book on the C Programming language would explain this and all details of the language. Studying the whole book and doing all the exercises in each chapter, would prepare you to program in C. I have listed my recommendations many times in this forum.
    sorry you answered whilst i was posting my drivel and put it a lot more succinctly

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    There is actually no such thing as a "String" in C.

    "String" is shorthand for "A Nul terminated array of type char".

    In the following statement:
    Code:
    char ary[10] = "ABCD";
    "ABCD" is a constant string, that cannot be altered.
    "ary[10]" is a 10 byte, or 10 char array, where the first 4 bytes contain a copy of the constant string, that can be altered. The 5th byte is the terminating Nul byte. You don't care what the remaining characters contain, though they are initialized to all Nul bytes, as part of the initialization.

    There are more details about single, and multidimensional arrays that a good book on C would instruct you.

    Again, please study a good up-to-date book on the C language, and stay away from YouTube videos, other online tutorials, or any book that claims to teach you C in a short time! They are useless, and usually limited and/or incorrect!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > However, the way pointers work with different data types can be confusing for me
    You have to remember that printf/scanf with "%s" coupled with a char* pointer is a special case.
    There's no hidden magic when it comes to arrays of any other type.

    Operating on a single char, and using the %c format, you can see the similarity.
    Code:
        char ch = 'a';
        char *y = &ch;
        printf("%c\n", *y); // Output: a
     
        int num = 10; // Define an integer variable 'num'
        int *x = &num; // 'x' points to the address of the integer variable 'num'
        printf("%d\n", *x); // Output: 10
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Store file to char double pointer?
    By thisisausername in forum C Programming
    Replies: 10
    Last Post: 11-24-2021, 03:03 PM
  2. Can pointer directly store data
    By Player777 in forum C Programming
    Replies: 4
    Last Post: 02-15-2020, 07:57 AM
  3. Replies: 4
    Last Post: 05-17-2009, 03:28 AM
  4. Pointer to String and Pointer to Char
    By vb.bajpai in forum C Programming
    Replies: 3
    Last Post: 06-15-2007, 03:03 PM
  5. Replies: 6
    Last Post: 11-29-2004, 08:50 AM

Tags for this Thread