Dennis
Dennis

Reputation: 939

array pointer function

I've been looking around for a solution to this, but haven't quite found one.

I've got a function which do some string manipulation (simplified):

void plr(char str, char *stro){
     strcpy(*stro, str);
}

My issue lies in the fact that I cannot get my result out from the function:

int main(void){
    //string and string out.
    char str[25], stro[25];
    printf("Something please: ");
    scanf("%s", &str);

    plr(str, &stro); // So basically stro would be the same as str.
    printf("Copy succesfull, %s", stro);
    return 0;
}

The whole idea is that I have the function pluralis, which would append pluralis to the string given and output it to stro. The whole string manipulation has been tested and works, if it's inside the main(), but I simply cannot get it to work with a function and the pointer. I could obviously leave it be, but what would I learn from that.

Is there something I need to consider when it's an array I point to, rather than a normal value of sorts.

Edit: Thanks for all the help, it has been solved. Greatly appreciated all!

Upvotes: 0

Views: 265

Answers (4)

ob_dev
ob_dev

Reputation: 2838

You should be doing this:

void plr(char str[], char stro[])
{
     strcpy(stro, str);
}

int main(void)
{
    char str[25], stro[25];
    printf("Something please: ");
    scanf("%s", &str); //unsafe code

    plr(str, stro); 
    printf("Copy succesfull, %s", stro);
    return 0;
}

Please be very careful when using a pointer to an array or a string for more :

  1. http://pw1.netcom.com/~tjensen/ptr/pointers.htm

Using scanf to get a string from the user is really a bad thing for more:

  1. Disadvantages of scanf

Upvotes: 2

BlackBear
BlackBear

Reputation: 22979

No need to mess up that way with pointers ;)

blackbear@blackbear-laptop:~$ cat prova.c
#include <stdio.h>
#include <string.h>

void foo(char *strin, char *strout)
{
    strcpy(strout, strin);
}

int main(void)
{
    char a[100], b[100];

    printf("What's a? ");
    scanf("%s", a);

    printf("What's b? ");
    scanf("%s", b);

    foo(a, b);

    printf("a is \"%s\"\nb is \"%s\"\n", a, b);
}
blackbear@blackbear-laptop:~$ gcc prova.c
blackbear@blackbear-laptop:~$ ./a.out 
What's a? abc
What's b? def
a is "abc"
b is "abc"
blackbear@blackbear-laptop:~$ 

Explaination:
This works because when you use the name of an array it decays to a pointer to its first element. So foo(a, b) actually is foo(&a[0], &b[0]). So, even if a and b are arrays, passing them to a function "converts" them to a pointer.
Quoting from here:

When you pass an array as an argument to a function, you really pass a pointer to the array's first element, because the array decays to a pointer.

and, a few lines below:

Decaying is an implicit &; array == &array == &array[0]. In English, these expressions read “array”, “pointer to array”, and “pointer to the first element of array” (the subscript operator, [], has higher precedence than the address-of operator). But in C, all three expressions mean the same thing.

Concluding, the problem in your code is just plr's prototype.

Look for array to pointer decay for more info about this phenomena. :)

Upvotes: 0

Dieter De Mesmaeker
Dieter De Mesmaeker

Reputation: 2156

I believe that you have to change the code to:

void plr(char* str, char *stro)

You want to pass in an array str, you have to use a pointer char *str.

I don't think you can pass arrays directly to functions.

if you declare char str[25]; the compiler reserves memory for the array, if you use char * it will just point to the first element in the array.

I hope this works!

Gr,

Dieter

Upvotes: 0

Perhaps you need a pointer to a pointer, i.e. declare your function void plr(char str, char **stro)

Upvotes: 0

Related Questions