Thread: Pointer assignment not working

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    2

    Pointer assignment not working

    Hi,

    I am new to C programming and understanding the concept of pointers.

    I have created a demo program of pointer for string operation as follow:

    Code:
    int main()
    {
    	char *p = "CODER C++";
    	p = p + 2;
    	*p = 'X';       // segmentation fault 
            printf("%c",*p); // Prints D
    	printf("%s",p);
    }
    But this is giving segmentation fault.
    if *p represents a character ( as it prints 'D' )value then I should be able to replace the value of *p with a "character" ..... but this is giving segmentation fault.
    Can anybody explain the reason and solution to this.

    Thanks
    Last edited by coderCPP1981; 06-20-2008 at 06:14 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    p points to (the first character, then third character, of) a string literal, so you are not allowed to assign to what it points to.

    More accurately, p should declared as a const char*, not a char*.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Make it
    char str[] = "CODER C++";
    char* p = str + 2;

    But, why is it "Coder C++" and not "Coder C"?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    2

    Wink

    But, why is it "Coder C++" and not "Coder C"?
    Actually earlier I decide to be C++ programmer but for C++ I should be good C programmer first.

    Thanks for explanation and will come back with more queries........

    I hope u will never mind it

    Thanks

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Actually earlier I decide to be C++ programmer but for C++ I should be good C programmer first.
    If you want to learn C++, then learn C++, not C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Languages like C++, C#, Java are based a lot on C in both style and logic.
    You can learn the basics of C, which will be useful on C++.
    BUT, pointers assignment is NOT basic stuff. So it is better to learn C++. If you know C it is much easier to learn C++. But you will also learn to do things that in C++ you would do differently. So why bother if you are going to program on C++?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I concur with Laserlight. There's absolutely no need to learn C before C++.
    A good C++ course will explain the low-level workings of machines (aka how you work in C), but in C you're stuck with low-level tools where in C++, you will learn high level tools with knowledge of how they work in low level.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 07:33 AM
  2. Replies: 1
    Last Post: 12-23-2008, 09:39 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Null pointer assignment
    By Raghavan in forum C Programming
    Replies: 1
    Last Post: 01-19-2008, 10:23 PM
  5. i have a pointer that it aint working. plz help.
    By adventurer in forum C++ Programming
    Replies: 7
    Last Post: 05-01-2002, 05:58 PM