Using pointer declared in other function, in main.

This is a discussion on Using pointer declared in other function, in main. within the C Programming forums, part of the General Programming Boards category; Hello, I've got the following code: Code: #include <stdio.h> void test(); int main() { test(); printf("%s",ptr); return 0; } void ...

  1. #1
    Novice.
    Join Date
    Oct 2005
    Posts
    88

    Using pointer declared in other function, in main.

    Hello,

    I've got the following code:
    Code:
    #include <stdio.h>
    void test();
    
    int main()
    {
    	test();
    	printf("%s",ptr);
    	return 0;
    }
    
    void test()
    {
    	char *ptr;
    	ptr = "Hello!";
    }
    Someone explained to me on IRC that string literals have static storage. So one would expect the preceding code to work. It however gives me a compiler error:

    Code:
    testcase.c: In function ‘main’:
    testcase.c:7: error: ‘ptr’ undeclared (first use in this function)
    Could somebody clarify this for me?

    Thank You

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    They do have static storage, but you aren't following scope rules.
    The ptr "ptr" only exists within test.
    A better example is to do this:

    Code:
    #include <stdio.h>
    const char* test();
    
    int main()
    {
    	printf("%s", test());
    	return 0;
    }
    
    const char* test()
    {
    	const char* ptr = "Hello!";
    	return ptr;
    }
    If this was a local array, it would not work, but for string literals, it will work.
    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.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > If this was a local array, it would not work, but for string literals, it will work.
    Yes, but it's a bad idea (at least I think so). The compiler can do what it wants really, "Hello!" might not even reside in read-only memory -- ie it could be moved out when test() returns.

    I'd avoid it
    Code:
    const char * test(void)
    {
        static const char str[] = "Hello!";
        return &str;
    }

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    I wouldn't place a function that returns a string literal in the first place
    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.

  5. #5
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    The const keyword makes a variable unchangable. I don't see what it does to a function. Does it change the value returned by the function in a read only variable?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    A string literal is (mostly) read only, which is why I use const.
    But const is const. The function returns const because the string literal is const. No biggie there.
    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.

  7. #7
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    What I actually want to achieve is that I want to return a string to the function main, and print it there. In my book I read the following example:
    Code:
    #include <stdio.h>
    
    void peasoup(int *green);
    
    int main()
    {
    	int turn;
    	int *head;
    	
    	turn=13;
    	head = &turn;
    	
    	peasoup(head);
    	
    	printf("Oh no it's &#37;d\n", *head);
    }
    
    void peasoup(int *green)
    {
    	while((*green)--) // -- gaat voor de * er wordt dus 1 afgetrokken en vervolgens de geheugenlocatie bekeken
    		puts("Blech!");
    	
    	*green = 13 * 51 + 3;
    }
    So I tried to apply it to strings.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I think I could be wrong though, otherwise:
    Code:
    const char * blah(void)
    {
        return "haha";
    }
    Wouldn't be right either. Perhaps someone smarter than us will come along

    ---------------

    > So I tried to apply it to strings.

    You could pass a pointer to a character array which you could then fill with the string...
    Code:
    void peasoup(char * str, size_t n)
    {
        if(str == NULL)
            return;
        memset(str, 0, n);
        strncpy(str, "Pea soup!", n - 1);
        return;
    }
    
    /* ... */
    
    char something[32];
    
    peasoup(something, sizeof(something));
    Last edited by zacs7; 07-03-2008 at 07:06 AM.

  9. #9
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42
    Quote Originally Posted by omnificient View Post
    Hello,
    Code:
    testcase.c: In function ‘main’:
    testcase.c:7: error: ‘ptr’ undeclared (first use in this function)
    Simply main does not see 'ptr' and also it exists only for while when test() is called.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    Code:
    #include <stdio.h>
    
    void peasoup(int* green);
    
    int main()
    {
    	int turn;
    	turn=13;
    	peasoup(&turn);
    	printf("Oh no it's &#37;d\n", turn);
    }
    
    void peasoup(int* green)
    {
    	while((*green)--) // -- gaat voor de * er wordt dus 1 afgetrokken en vervolgens de geheugenlocatie bekeken
    		puts("Blech!");
    	
    	*green = 13 * 51 + 3;
    }
    Cleaned.
    No need to create a temporary pointer at all.
    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.

  11. #11
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    I appreciate your help, but your code often contains concepts I don't understand. Why won't the following code work:
    Code:
    #include <stdio.h>
    void test(char *green);
    
    int main()
    {
    	char *green = "Hello\n";
    	test(&green);
    	printf("%s",green);
    	return 0;
    }
    
    void test(char *green)
    {
    	*green = "Hi\n";
    	return;
    }
    I get the following errors:
    Code:
    testcase.c: In function ‘main’:
    testcase.c:7: warning: passing argument 1 of ‘test’ from incompatible pointer type
    testcase.c: In function ‘test’:
    testcase.c:14: warning: assignment makes integer from pointer without a cast
    Can't I pass the function the memory adress and then let the function use that to modify the data? That sounds like a lot less complicated solution actually.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    First, I told you it should be const char*.
    And then let me ask you two questions:
    1) What happens when you dereference char*?
    2) What is the type when you take the address of a variable?

    If you can understand those concepts, you should able to understand why the code is wrong.
    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.

  13. #13
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    You have two options:

    1) Return a newly allocated pointer.
    2) Pass a reference to the original pointer to the function. (So the function will accept a **)

    Don't give up, struggle until you can't anymore... Then struggle some more

  14. #14
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You are confusing the uses of * and & for pointers. If you have a variable the & in front of it gives you the adress. If you have a pointer then you already got the adrress.
    So when a function needs a pointer to char as a parameter you either give a pointer or the address of a variable. Clear?

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    Hey now, don't spoil the solution.
    Actually, let me ask: what happens when you pass a pointer to a function that expects a pointer?
    How is the contents of the pointer passed?
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Replies: 15
    Last Post: 12-07-2006, 08:27 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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