changing the value pointed to by a pointer

This is a discussion on changing the value pointed to by a pointer within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> void alphabet(char *); int main(void) { alphabet("a"); system("PAUSE"); return 0; } void alphabet(char *p) { if(*p <= ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63

    Question changing the value pointed to by a pointer

    Code:
    #include <stdio.h>
    
    void alphabet(char *);
    
    int main(void) {
        alphabet("a");
        
        system("PAUSE");
        return 0;
    }
    
    void alphabet(char *p) {
        if(*p <= 'z') {
            putchar(*p);
            (*p)++;
            alphabet(p);
        }
    }
    Can someone tell me why, this won't work. No messages when compiled.

    I this, its because you can't change a string from the program's string table if that makes any sense.

  2. #2
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Im not sure, but if you change your main function to look like this, it'll work:

    Code:
    char ch = 'a';
    alphabet(&ch);
    I think it may have something to do with the fact that you are sending a literal value, instead of the address of a variable and then trying to change it. That is a very big guess though and I imagine I'm wrong.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Close. The OP is right. You cannot modify string literals. Or rather, you shouldn't try. Consider:
    Code:
    char *s = "point to s at a string literal";
    ...
    alphabet( s ); /* will have the same problem... */
    Here, even though we use a variable, and pass that, it still is referring to a string literal. You have to use an array, or dynamically allocate memory, fill it, and pass that...
    Code:
    char *s = malloc( strlen( "allocate some space" ) + 1 );
    strcpy( s, "allocate some space" );
    ...
    alphabet( s ); /* works, because 's' doesn't point at a string literal */
    Or...
    Code:
    char s[] = "an array of stuff";
    ...
    alphabet( s );
    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Cool, I finally feel like I've actually learnt things over the last few months.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    > Can someone tell me why, this won't work
    How can you say that when you never said what it is you expect to happen.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    >How can you say that when you never said what it is you expect to happen.

    Am I allowed to point out that the code is pretty simple and a person of your ilk should be able to deduce what it is trying to achieve?

    No? Ok. .

    Though, I can see where you're coming from.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    Fixing it for semantic correctness is not a problem, any half-trained chimp could manage that.
    But very minor changes to the sense of the ++ line of code will radically alter the functionality.

    I've been down the "yeah thanks, but I wanted bar to happen, not foo", having previously seen no hint of either.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Yes, fair enough, I see your point.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing a char pointer via DLL
    By rockpaandi in forum C Programming
    Replies: 5
    Last Post: 04-11-2009, 11:16 PM
  2. changing the address of a pointer question..
    By transgalactic2 in forum C Programming
    Replies: 42
    Last Post: 10-16-2008, 09:20 AM
  3. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  5. changing mouse pointer in c
    By MMM in forum Windows Programming
    Replies: 3
    Last Post: 05-11-2003, 08:28 PM

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