Pls clarify the doubt in pointers...

This is a discussion on Pls clarify the doubt in pointers... within the C Programming forums, part of the General Programming Boards category; Hi, I'm not able to understand why the below code is not working properly. 1 #include 2 void f(char *s) ...

  1. #1
    sguruprasanna
    Guest

    Unhappy Pls clarify the doubt in pointers...

    Hi,
    I'm not able to understand why the below code is not working properly.

    1 #include
    2 void f(char *s)
    3 {
    4 s="woooo";
    5 }
    6 void main()
    7 {
    8 char a[] = "Hello";
    9 f(a);
    10 printf(a);
    11 }

    In the function f, i'm trying to assig a value to the char pointer passed from main(). I don't understand why this value is not getting reflected in main(). But if i replace the line
    4 from
    s="woooo";
    to strcpy(s,"woooo");

    the value is getting reflected in main(). Any help would be appreciated.

    Thanks & regards,
    S.Guruprasanna

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    If you declared the string as char* in main, and did this:
    Code:
    void f(char *s) 
    { 
        s="woooo"; 
    }
    It won't be reflected in main, because :
    You are making the pointer point somewhere else, you aren't changing the pointed-to-data, which is the string.

    When you pass a string, always use strcpy() to change the pointed to data.

    If you wanted the make the pointer point somewhere else in memory, then do on of the following:

    Return a pointer to the new address and assign it.
    OR
    Pass the pointer by reference.(Pass it's address to the function)

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    13

    Re.. doubt about pointers

    babu...
    u should pass by address in-order to change the value of a variable defined in one function , in another function.

    but u r passing the variable by value.

    instead if u pass by address, u can change the variable in function f.

    check out the following code:

    #include<stdio.h>

    void f(char **c)
    {
    *c = "chandra";
    }


    int main()
    {
    char *c;
    f(&c);
    printf("%s",c);
    getch();
    return 0;
    }

    hope this wil clear ur doubt.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    ucsbme, no. You could do it this way but it would still be wrong. Listen to what the Dog said. Here is what the function should look like
    Code:
    void f(char *s, int max_size) {
         strncpy(s, "whooo", max_size);
    }
    The reason you want to have the second parameter is because you have no other way of knowing the size of the buffer that is pointed to by "s." Also remember
    Code:
    void f(char *s) {
        s = "whooo";
    }
    //and
    void f(char **s) {
        *s = "whooo";
    }
    both point s to a temporary array. In other words "s" is only valid until the function terminates. Use strcpy or something like this function.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by master5001
    Also remember
    Code:
    void f(char *s) {
        s = "whooo";
    }
    //and
    void f(char **s) {
        *s = "whooo";
    }
    both point s to a temporary array. In other words "s" is only valid until the function terminates. Use strcpy or something like this function.
    Not exactly true..... the first example code will use the f() functions copy of s, therefore it will not affect the callers copy of s. In this case your point is correct.

    However, the second example code passes by address, therefore the code is correct, and the callers copy of the pointer will be updated to point to the new string literal.

    However, depending on what is going on in the app elsewhere, I'd prefer to pass a buffer pointer and length, and use strncpy(), as per your own example.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  4. help me pls..... :(
    By mocha_frap024 in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2002, 09:46 AM

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