9

Can anyone explain to me what I don't understand here please?

I am trying to pass the argument as a "string" (i know there are no strings in c) so that I can use that string later with other functions like it's a file name that has to be passed for example. But I don't know why it won't accept it or what type should it be

#include <stdio.h>

int main ( int argc, char *argv[] )
{
    char *array= argv[0];
    foo(*array);
}

void foo( char *array) 
// notice the return type - it's a pointer
{
    printf(array);
}

thanks alot!

Ryan
  • 269
  • 4
  • 5
  • 12
  • you dereference array while passing it to foo. So you only pass the very first byte value if it is a c string literal – Lefteris Jul 30 '12 at 01:41
  • You should avoird expression such as `printf(s)` because it's an uncontrolled format string. – md5 Jul 30 '12 at 06:09

3 Answers3

18

You should be calling the function like this:

foo(array);

What you're doing is dereferencing the pointer, which returns a char, which is the first character in the string.

Your printf call should also look like this:

printf("%s", array);

Your entire fixed code should look like the following:

#include <stdio.h>

void foo(char *array)
{
    printf("%s", array);
}

int main ( int argc, char *argv[] )
{
    // TODO:  make sure argv[1] exists
    char *array= argv[1];
    foo(array);
}
  • i removed the asterix like you said, it still doesn't work! It says: redefinition: different basic types – Ryan Jul 30 '12 at 01:45
  • i did both fixes, still says same thing and won't compile! – Ryan Jul 30 '12 at 01:49
  • 1
    @Ryan: Specifically, what error message are you getting from your compiler? –  Jul 30 '12 at 01:51
  • 'foo':redefinition; different basic types – Ryan Jul 30 '12 at 01:53
  • that excatly with the first line of the function as the location of the error – Ryan Jul 30 '12 at 01:53
  • the entire fixed code won't compile, would it compile with you? thanks! – Ryan Jul 30 '12 at 01:58
  • @Ryan, It compiles fine one ideone. The only difference is that wants a `return 0;` for runtime and you can't give it `argv[1]`. It works fine if that's changed to a string literal, though. – chris Jul 30 '12 at 02:00
  • thank you so much for all your time and effort, i really appreciate it, it works!! :) thanksssssssssssssssssssssssssssssssssssssssssss – Ryan Jul 30 '12 at 02:01
5

When you say foo (*array), you're decaying the array into a pointer to the first element, in order to dereference that element, giving you the first character. That's what you're trying to pass to the function. Leave out the asterisk and just pass array for it to decay into the pointer you need.

The other issue is that you're not using printf correctly. First of all, here's a reference. You need to pass a string of tokens that tell the compiler what type of argument to expect next because it has no way of telling. In your case, your string would contain "%s" to tell it to expect a char *, and then you'd pass array as that char * argument.

printf ("The string is %s", array);
chris
  • 60,560
  • 13
  • 143
  • 205
  • i removed the asterix like you said, it still doesn't work! It says: redefinition: different basic types – Ryan Jul 30 '12 at 01:46
  • i noticed you reedited to how it was before? Shall there be an asterix in the argument being passed to the function or not? Actually i just noticed with or without it, it won't work – Ryan Jul 30 '12 at 01:51
  • @Ryan, You shouldn't be dereferencing the decayed array anywhere. It's already decayed into the pointer you need when it gets passed into a function. – chris Jul 30 '12 at 01:52
1

argv is a array of character pointers, that means argv is going to store the address of all the strings which you passed as command line argument.

so argv[0] will gives you the address of first string which you passed as command line argument, that you are storing into the pointer variable array in main function.

Now you have to pass only the address to the function foo but you are passing the first character of that string. For example if your first command line argument is temp.txt you are passing character t to the function foo. So inside foo function you are having a char pointer variable array, in that ASCII value of the character t will be assigned. And then you are passing that to printf, which will treads that ASCII value as address, and it will tries to access that address to print which will leads to crash (unexpected behaviour).

So you have to pass only the address of the command line argument to the function foo like below.

foo(array);

printf(array) - Here printf will treads the format specifier as string(%s) and it will tries to print all the characters starting from the address array untill it meets a null character \0.

But better to add the printf like below

printf("%s", array);

rashok
  • 12,790
  • 16
  • 88
  • 100