Strings and pointers

This is a discussion on Strings and pointers within the C Programming forums, part of the General Programming Boards category; Hi guys Please take a look at the two lines of code Code: char asd[][4]={"qwe", "rty"}; and Code: char *asd[]={"qwe", ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    140

    Strings and pointers

    Hi guys

    Please take a look at the two lines of code

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

    Code:
    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?

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

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    33
    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, 10: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, 08:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21