Thread: char pointer and int pointer

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    17

    char pointer and int pointer

    these are valid :
    Code:
    char str[5]="good";
    int a[3]={1,2,3};
    
    char *p="hello";
    but why is this wrong then :

    int *q={1,2,3}; when char *p="hello"; is valid.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Character arrays and pointers were always a special case in C.

    Further, some compilers (GCC) permit compond literals in the style you seek.
    Compound Literals - Using the GNU Compiler Collection (GCC)
    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.

  3. #3
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Code:
    char *p = "Hello";
    What this does is create a character array "Hello" on the stack, and points p to the beginning of it. This is also why you can't do something like p[0] = 'J'; to make it say "Jello", because "Hello" was allocated on the stack (read only), and you would get a segfault at execution.

    Code:
    char *p = { 'H', 'e', 'l', 'l', 'o', '\0' };
    Which is the equivalent of your integer example, does not work.

    Simply put, the behavior of "Hello" is different than the behavior of actually assigning values to elements in an array. These are just pointers, they point to values, they are "scalar" meaning they hold one value, which is the pointer to the location they point to! If you declare it an array, your compiler knows it's not a scalar, thus you can assign things like that.

    If you have an array or pointer, and you call forth array[1], that just means *(array+1). It goes to array[0]'s memory address and then goes one unit (integer, character, etc) forward.

    Also, you can do something like this if you want to:
    Code:
    int p[] = { 1, 2, 3, 4 };
    Last edited by Phenax; 03-07-2011 at 01:11 PM.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Phenax View Post
    Code:
    char *p = "Hello";
    What this does is create a character array "Hello" on the stack, and points p to it. This is also why you can't do something like p[0] = 'J'; to make it say "Jello", because "Hello" was allocated on the stack (read only), and you would get a segfault at execution.
    C doesn't actually specify where in memory to store stuff or what permissions that page/segment should have, but generally, string literals are not stored on the stack. The stack is typically readable and writable, but not executable. It's where local variables and function parameters get put, and since those variables can change, it must be writable. String literals would likely be put in a read-only section of the global data section, even if the pointer itself is a local variable (in which case the 4 bytes or whatever for p would be put on the stack, but not the string it points to).

  5. #5
    THANK YOU KINDLY SIR Phenax's Avatar
    Join Date
    Mar 2011
    Posts
    74
    Quote Originally Posted by anduril462 View Post
    C doesn't actually specify where in memory to store stuff or what permissions that page/segment should have, but generally, string literals are not stored on the stack. The stack is typically readable and writable, but not executable. It's where local variables and function parameters get put, and since those variables can change, it must be writable. String literals would likely be put in a read-only section of the global data section, even if the pointer itself is a local variable (in which case the 4 bytes or whatever for p would be put on the stack, but not the string it points to).
    Oops, sorry about the misinformation then. Thanks for the clarification.
    Quote Originally Posted by Plato
    Never discourage anyone...who continually makes progress, no matter how slow.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM