Thread: Please Prevent my Pointer Predicament.

  1. #1
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212

    Please Prevent my Pointer Predicament.

    I'm having some trouble with pointers and arrays, here's my code:
    Code:
    /*------------------------\ 
    | I HATE POINTERS ARRGGGG |
    |This program serves no   |
    |purpose except so I can  |
    |be told what is wrong    |
    |with it, because I cant  |
    |figure out these damn    | 
    |pointers!                |
    |Brian - 2002             |
    \------------------------*/
    
    #include <stdio.h>
    #include <string.h>
    
    int changeToHello(char *thingToChange[BUFSIZ])
    {
     strcpy(*thingToChange,"hello");
     return 0;
    }
    
    int main(void)
    {
     char myString[] = "I like to read books";
    
     printf("\n%s",myString);
     changeToHello(&myString);
     printf("\n%s\n",myString);
    
     return 0;
    }
    You can see what it's supposed to do, but how do I fix it? Help is appriciated, Bri.

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Hmmm...try changing it to this and see if it works as wanted:
    Code:
    /*------------------------\ 
    | I LOVE POINTERS ARRGGGG |
    |This program serves no   |
    |purpose except so I can  |
    |be explain what is wrong |
    |with it, because I can   |
    |figure out these darn    | 
    |pointers!                |
    |Garfield - 2002          |
    \------------------------*/
    
    #include <stdio.h>
    #include <string.h>
    
    int changeToHello(char *thingToChange)
    {
     strcpy(thingToChange,"hello");
     return 0;
    }
    
    int main(void)
    {
     char myString[] = "I like to read books";
    
     printf("\n%s",myString);
     changeToHello(myString);
     printf("\n%s\n",myString);
    
     return 0;
    }
    1978 Silver Anniversary Corvette

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Did it work? Is this what you wanted? Would you like me to explain what I changed and why?
    1978 Silver Anniversary Corvette

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    hah! thanks.

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    No problem. Glad I could help
    1978 Silver Anniversary Corvette

  6. #6
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Changing the commented area fixed the bug! I knew it was "darn" but I kept thinking "damn"

  7. #7
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Originally posted by Brian
    Changing the commented area fixed the bug! I knew it was "darn" but I kept thinking "damn"
    LOL
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 08:23 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM