please help a beginner

This is a discussion on please help a beginner within the C Programming forums, part of the General Programming Boards category; char* p; p = foo( ); ... ... char * foo() { char temp[20]; ... ... return temp; } how ...

  1. #1
    karen66
    Guest

    Unhappy please help a beginner

    char* p;
    p = foo( );
    ...
    ...

    char * foo() {
    char temp[20];
    ...
    ...
    return temp;
    }

    how can this function be modified to return a ‘char *’ from the function, and an additional ‘char *’ value in one function call even if the size of the char * desired is not known in the outside calling function.

    Thanks,

  2. #2
    karen66
    Guest
    But, where is the additional char* value being returned ? Or how we can get a second ‘char *’ to use back from this function?

    Thanks,

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    For starters:

    Code:
    char * foo() {
    	 char temp[20];
    	 return temp;
     }
    Is just going to crash/segfault your program sooner or later.

    As for your real question, there are a few things you can do.

    You can have the user pass pointers to the strings you want to return, something like

    Code:
    void foo(char *retval1, char *retval2)
    {
    	retval1 = some junk;
    	retval2 = some junk;
    }
    Then, when your program returns, variables they passed to your function will hold some junk.

    Another choice, which probably isn't the best, is using structures.

    Code:
    struct my2strings
    {
    	char *string1;
    	cahr *string2;
    }
    
    struct my2strings *foo(void)
    {
    	struct my2strings ret = malloc(sizeof(struct my2strings));
    	ret->string1 = some junk;
    	ret->string2 = some junk;
    	return ret;
    }
    Hope that helps.

  4. #4
    Registered User plungermonkey's Avatar
    Join Date
    Dec 2002
    Posts
    6

    Lightbulb Re: please help a beginner

    Originally posted by karen66
    char* p;
    p = foo( );
    ...
    ...

    char * foo() {
    char temp[20];
    ...
    ...
    return temp;
    }

    how can this function be modified to return a ‘char *’ from the function, and an additional ‘char *’ value in one function call even if the size of the char * desired is not known in the outside calling function.

    Thanks,
    Hey Karen66!

    Lemme see if I get the gist of your post. You, in effect, want to return two pointer values in one function. Assuming this is the case, I don't really think you can. However, what about this. Make the pointer to the address of foo() global. This way all of the functions needing access to it can without problems. You can then assign a pointer within foo() to handle the elements within temp[20]. It'd look something like this...

    #include <stdio.h>

    char foo(void);

    char* pf;
    pf = foo();

    int main()
    {
    .....
    .....
    blabla = foo();
    .....
    .....
    }

    char foo(void)
    {
    char* pt;
    char temp[20];
    pt = temp[];

    <insert element selection code here>

    return pt;
    }

    With all that said, I hope this is what you were wanting to do.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char foo(void)
    {
    char* pt;
    char temp[20];
    pt = temp[];
    
    <insert element selection code here>
    
    return pt;
    }
    This is entirely wrong. Here's why:

    1) Your function returns a single character, not a pointer.
    2) 'temp' has local scope, and since it isn't static, it's gone when the function returns.
    3) The assignment line 'pt = temp[];' is incorrect.

    Correct, it would be: 'pt = temp;' or 'pt = &temp[0];'

    Better written would just be to not even bother using 'pt', and as Salem's example using a static array suggested, just return the name of the array.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User plungermonkey's Avatar
    Join Date
    Dec 2002
    Posts
    6
    Thanks for the correction Quzah. The brain didn't correctly parse what Karen66 was wanted to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  3. Best Free Beginner Online Books/Tutorials?
    By Zeusbwr in forum C++ Programming
    Replies: 2
    Last Post: 10-12-2004, 05:52 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 10:21 AM

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