Thread: C function argument and return values

  1. #1
    Registered User
    Join Date
    Apr 2023
    Posts
    4

    C function argument and return values

    Dear Sirs,
    This is my first message in this forum. I want to make this easy for all. I am struggling with an issue in VSCode in Windows 11
    For a long time studied with VSCode in Ubuntu just did some codes after a while for migration for cross platforms just passed to the Windows. For describing this issue i am giving a small code sample to define what i have been encountering in VSCode. Just summurizing the issue with an code:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    int foo(int *value,char **text){
        char a[]="hello World";
        *text=a;
        *value=2;
        return(0);
    }
    int main(){
        int a=1;
        char *ptr;
        foo(&a,&ptr);
        printf("%d %s",a,ptr);
        getchar();
        return(0);
    }
    While taking a refference from a function previously there were any kind of problem in Ubuntu. The code above was working so clear. So far so good then try to perform the same code in Windows. When i run the code accepted output value must be:
    "hello World" but in Windows just taking the "2 hell" console output. Obviously i have an headace in Windows now. For clearifying event i captured the last machine status of VSCode debugger for revealing what i am doing wrong or right. Any ideas or options for this issue
    I would be very glad for the assist

    C function argument and return values-untitled-jpg
    After calling the function and returning back to the main loop there are any kind of variable error till using the printf function.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Your code has "undefined behavior" which means it may seem to work in some cases (like on a different operating system, or with a different compiler, or whatever) but it is still incorrect even if it seems to work.

    The issue is that you are returning a pointer to a local variable in foo. Local variables cease to exist when the function returns so the value of that variable is no longer stable and should not be accessed.

    There are multiple ways to fix it depending on what you are trying to achieve.
    Code:
    #include <stdio.h>
    #include <string.h>
     
    // You could make 'a' static.
    void foo(char **text){
        static char a[] = "Hello World";
        *text = a;
    }
    char *foo_ret(){
        static char a[] = "Hello World";
        return a;
    }
     
    // You could return the address of a string literal.
    void foo2(char **text){
        *text = "Hello World";
    }
    const char *foo2_ret(){
        return "Hello World";
    }
     
    // You could copy the string data to a char array (of sufficient size) in the caller.
    void foo3(char *text, size_t sz){
        strncpy(text, "Hello World", sz);
    }
     
    // You could dynamically allocate memory and return that pointer.
    void foo4(char **text) {
        *text = malloc(32);
        strcpy(*text, "Hello World");
    }
    char *foo4_ret() {
        char *p = malloc(32);
        strcpy(p, "Hello World");
        return p;
    }
     
    int main(){
        char *ptr;
     
        foo(&ptr);
        printf("%s\n", ptr);
        ptr = foo_ret();
        printf("%s\n\n", ptr);
     
        foo2(&ptr);
        printf("%s\n", ptr);
        const char *p2 = foo2_ret();
        printf("%s\n\n", p2);
     
        char s[32];
        foo3(s, sizeof s);
        printf("%s\n", s);
     
        foo4(&ptr);
        printf("%s\n", ptr);
        free(ptr); // Remember to free dynamically-allocated memory.
     
        ptr = foo4_ret();
        printf("%s\n", ptr);
        free(ptr);
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-09-2016, 11:39 AM
  2. Replies: 4
    Last Post: 06-10-2013, 04:03 PM
  3. Replies: 12
    Last Post: 05-24-2011, 05:57 AM
  4. String Argument and Return Values From DLL
    By C0D3BR3AK in forum Windows Programming
    Replies: 0
    Last Post: 04-28-2011, 11:50 AM
  5. can a function return 2 values?
    By panfilero in forum C Programming
    Replies: 2
    Last Post: 09-27-2005, 02:35 AM

Tags for this Thread