0

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!

Dennis
  • 909
  • 4
  • 13
  • 30
  • 2
    str must be a pointer, so first of all, you prl function should be `void prl(const char *str, char *stro)`, and since str is the source, it should be passed const. – Aif Nov 01 '11 at 17:43

4 Answers4

2

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
Community
  • 1
  • 1
ob_dev
  • 2,808
  • 1
  • 20
  • 26
0

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

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

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

Dieter De Mesmaeker
  • 2,156
  • 2
  • 14
  • 14
  • You can pass arrays "directly" to functions, but they pass by reference anyway. – Mooing Duck Nov 01 '11 at 18:02
  • I stated unclearly, sorry. I once tried using stuff like void plr(char str[], char stro[]); which didn't work ^^ That was what I was trying to say with 'directly passing them' :) – Dieter De Mesmaeker Nov 01 '11 at 18:52
0

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. :)

BlackBear
  • 22,411
  • 10
  • 48
  • 86
  • That works great, however, I do not understand why you all the sudden don't need the & symbol in front of the output, like you normally would (at least that's what I've been taught). If you could elaborate it would be greatly appreciated. – Dennis Nov 01 '11 at 17:54
  • @Kola: He's doesn't need the `&` because he's not modifying the pointer, and just takes a copy of the pointer. Instead he's modifying what the pointer is pointing at. – Mooing Duck Nov 01 '11 at 18:03
  • Edit your answer so that it can be readable, then ask people to read it. – littleadv Nov 01 '11 at 18:51