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

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    96

    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.

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