pointer does not get assigned...

Posted on 2005-04-30
Last Modified: 2010-04-15

I do:

char str[10]="abcd";
char *sptr = str;


This works...


char *sptr = "abcd";
printf("%s\n", sptr[2]);   //This works

sptr[2] = 'p';

This does not work...

I don't get the difference between these 2... they both are pointers and are pointing to a string...

can someone throw some light on this....

Question by:srinivas_vemla
    LVL 55

    Assisted Solution

    by:Jaime Olivares
    printf("%s\n", sptr[2]);   //This works

    This wouldn't work since printf() is expecting a string and you pass it a character

    sptr[2] = 'p';
    This may produce a violation because sptr is pointing to a constant string when you declared:
    char *sptr = "abcd";
    LVL 10

    Accepted Solution

    AFAIK, (or atleast how C++ handles char strings)

    when u say
    char str[10] = "abcd" ;

    A char string of 10 locations is allocated, string abcd is copied into that location
    Apart from this, since "abcd" is a string literal,  a separate constant memory is created
    Thus, initially, this constant memory is created and then its contents are copied in your str of 10 locations

    Thus, sting str itself is not a constant location.
    Hence can be changed

    Now, when  u say
    char *str = "abcd" ;

    str is just a pointer pointing to the constant string.
    Hence when u try to modiy this location, u get a runtime error because u are modifying a conatnt location


    Author Comment

    Thanks jaime,

    Yeah, that was a typo on the printf() line...

    So, do you mean that since I assigned sptr to "abcd", I cannot change a single character in it again?... But how come I can change if I declare the string like:

    char s[] = "abcd";
    s[2] = 'p';  //This works
    I don't get the difference clearly...
    LVL 10

    Expert Comment

    As I said earlier
    When u declare
    char s[] = "abcd"

    There r 2 steps

    1. COnstat string allocation in memmory
    2. COpying of this string char-by-char into s (a separarte memory location which is not const)

    Thus, u can change s but not the other one

    LVL 30

    Assisted Solution

    >>char s[] = "abcd";

    In above code s is not pointing to a string literal.  The compiler will allocate memory the size of the string literal, and then copy the contents in the string literal into s

    So 's' can be modified, since it's not pointing to a string literal.

    char *x = "abcd";

    In above line, x is pointing to a string literal.  Changing a string literal is considered undefined behavior according to both C and C++ standards.

    Some compilers will let you change it, and some compilers will put the string literal in constant memory, and therefore you'll get a run time error.
    When declaring a pointer that points to a string literal, you should always declare it constant, so you can avoid accidently modifying it.

    const char* x = "abcd";

    Author Comment

    Thanks guys,

    That makes it clear...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now