0

I have a library which has its own command-line arguments parsing mechanism when the library is compiled as stand alone binary and here is the code:

int main( int argc, char **argv )
{
  int result;

  argc = wool_init( argc, argv );
  ....
}

int wool_init( int argc, char **argv )
{

  us_elapsed();

  argc = decode_options( argc, argv );
  .....
}

Now I am linking this library statically into another one which has its own Command-line parsing mechanism and arguments. I would like to initialize this first library with some arguments at run-time for example I am passing these arguments as follows to mimic command-line:

/* initializing wool run-time environment */
char **woolArg;
*woolArg[0] = "-p 3";
wool_init(1, woolArg);

But I am getting following error.

:113:14: error: assignment makes integer from pointer without a cast [-Werror]
  *woolArg[0] = "-p 3";
              ^

Can somebody please help?

Black_Zero
  • 445
  • 1
  • 6
  • 12
  • 1
    Didn't down vote, but it's probably because you could have answered the question yourself by searching for that error message, which yields plenty of relevant questions here on StackOverflow dealing with that issue. – DarkDust Mar 31 '16 at 12:59

3 Answers3

6

Let's count the stars.

char **woolArg;

Two stars at declaration.

woolArg[0]

This uses up one star. We're left with a char*.

*woolArg[0]

This uses up another star. We're left with a char.

*woolArg[0] = "-p 3";

There is a char* on the right side of the assignment. char and char* are two very different things. You may want to try

woolArg[0] = "-p 3";

... except it won't work, because woolArg is uninitialized and so you cannot touch woolArg[0]. But instead of trying to fix that, only to get stuck at the next problem, and then the next one and one after that, learn the simple and correct form:

char* woolArg[] = { "library", "-p", "3", NULL };

The first string is arbitrary. You can use argv[0] here if it is accessible.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
2
woolArg = malloc(sizeof(char *) * 3/*number of args*/);

woolArg[0] = /*program name*/;
woolArg[1] = "-p";
woolArg[2] = "3";
Boiethios
  • 38,438
  • 19
  • 134
  • 183
  • The op has also forgotten space for `woolArg` – Nim Mar 31 '16 at 12:44
  • 1
    Why the `strdup`? That's not necessary for string literals (as long as you're not going to iterate over the array to `free` all entries). – DarkDust Mar 31 '16 at 12:45
2

Let's put the memory management issues aside, other answers here already deal with that. Let's explain the error message. Your statement is:

*woolArg[0] = "-p 3";

What are the types?

  • woolArg is a char **
  • *woolArg is a char *
  • *woolArg[0] is a char
  • "-p 3" is a char[] but decays into a char *

So the left side of your assignment expects a char, but the right side provides a char *. Hence the warning/error that you are implicitly turning a pointer into an integer.

To fix this, you need get rid of an indirection:

woolArg[0] = "-p 3";
Community
  • 1
  • 1
DarkDust
  • 90,870
  • 19
  • 190
  • 224