Pointer assignment not working

This is a discussion on Pointer assignment not working within the C Programming forums, part of the General Programming Boards category; Hi, I am new to C programming and understanding the concept of pointers. I have created a demo program of ...

  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
    21,647
    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*.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    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
    21,647
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    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, 08: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, 09: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

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