Thread: Strings and pointers

  1. #1
    Registered User
    Join Date
    Aug 2009

    Strings and pointers

    Hi guys

    Please take a look at the two lines of code

    char asd[][4]={"qwe", "rty"};

    char *asd[]={"qwe", "rty"};
    When using the first line, I am able to change the values of asd[x][y], while I am not allowed to change the values of the array in the second example. Why is that?

    Last edited by Niels_M; 07-17-2010 at 03:49 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    The second one is an array of pointers, which point to string literals. The first is a 2D array of characters, initialized with those values.

    Question 6.2

    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2010
    When a pointer points to a string literal, the string is immutable ( cannot be overwritten ). However when an array is set up with the string literal, it's placed on mutable memory on the stack.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers with strings and stuff.
    By DLH112 in forum C Programming
    Replies: 9
    Last Post: 10-21-2009, 04:51 PM
  2. Help with calloc and pointers to strings.
    By sweetarg in forum C Programming
    Replies: 1
    Last Post: 10-24-2005, 02:28 PM
  3. Arrays, pointers and strings
    By Apropos in forum C++ Programming
    Replies: 12
    Last Post: 03-21-2005, 11:25 PM
  4. Concatenating strings (dynamic array using pointers)
    By Tankndozer in forum C Programming
    Replies: 8
    Last Post: 07-01-2004, 07:27 AM
  5. hangman need help with strings and pointers
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-27-2002, 09:13 AM