0

I would like to find out how to get this code to work, my printf in the main function will not print the "test" string.

#include <stdio.h>

int main()
{
char b[10];
test(b);
printf("from main func: %s\n", b);
}

int test(char* buf)
{
char len[] = "test";
char *pt = len;

printf("printing ptr: %s\n", pt);

buf = pt;

printf("from test func: %s\n", buf);

return 1;
}
d123
  • 1,497
  • 2
  • 12
  • 22
  • 3
    Prototypes missing. Enable compiler warnings and pay heed to them. Also use a standard compliant or at least C99 compiler. – too honest for this site Jan 09 '16 at 19:29
  • `buf = pt;` --> `strcpy(buf, pt);` – BLUEPIXY Jan 09 '16 at 19:31
  • 1
    No offence: Please read about pointers and types. Your code shows major missconception of the basics. – too honest for this site Jan 09 '16 at 19:31
  • 1
    In `int test(char* buf)` the `buf` is a *copy* of the variable that was passed. You can change its value locally, but it will not find its way back to the variable whose value was passed as the function argument, and even if you find a way to do that, you passed an array (decayed to a pointer) so you can't change `b`. And also, the data you wanted `b` to point to, has gone out of scope on return from the function. – Weather Vane Jan 09 '16 at 19:37
  • The cause is like when "Dangling Pointer" occur. See http://stackoverflow.com/questions/17997228/what-is-a-dangling-pointer and https://en.wikipedia.org/wiki/Dangling_pointer – Hamid Rouhani Jan 09 '16 at 19:47

3 Answers3

0

First of you need to have a prototype for the test function or place it above main.

#include <stdio.h>
#include <string.h>

int test(char *);

int main()
{
char b[10];
test(b);
printf("from main func: %s\n", b);
}

int test(char* buf)
{
char len[] = "test";
char *pt = len;

printf("printing ptr: %s\n", pt);

strcpy(buf, pt);

printf("from test func: %s\n", buf);

return 1;
}

Also you should never try to pass a local variable to a function. It will not work, thus you have to use strcpy(buf, pt); instead of pointing the local variable len to buf.

Linus
  • 1,516
  • 17
  • 35
0

Instead of assigning pt to buf you need to copy the string to buf:

strcpy(buf, pt);

Here is a safer version of your code:

#include <assert.h>
#include <stdio.h>
#include <string.h>

#define LEN(arr) (sizeof (arr) / sizeof (arr)[0])

void test(char *buf, int bufLen)
{
    char len[] = "test";
    char *pt = len;

    assert(bufLen > strlen(len));

    printf("printing ptr: %s\n", pt);
    strcpy(buf, pt);
    printf("from test func: %s\n", buf);
}


int main()
{
    char b[10];

    test(b, LEN(b));
    printf("from main func: %s\n", b);
    return 0;
}
August Karlstrom
  • 10,773
  • 7
  • 38
  • 60
0

You cannot use buf = pt; in C. The reason being is that C sees "test" as a list of individual characters. like 't', 'e', 's', 't'. Like an array of integers, you could not have something like...

int myArray[5] = { 0, 1, 2, 3, 4 };

and then have something like...

int newArray[5] = myArray... that just wouldn't work, well, an array of characters is no different. You need to copy each character over to the new array, just like you would with an array of ints. You could write your own function to do this, or use one of the built in functions that are available like strcpy() which will copy them for you.

Neil Roy
  • 603
  • 5
  • 13