LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   "...incompatible pointer type" Can it be ended? (https://www.linuxquestions.org/questions/programming-9/incompatible-pointer-type-can-it-be-ended-348075/)

lyd 07-29-2005 12:04 PM

"...incompatible pointer type" Can it be ended?
 
Its probably been asked and answered so many times now that only the last
several responsesare coming up on the search button, and those are direct
answers to offshoot problems that include this warning in their compilations,
I want to know, once and for all, what the compiler does this for. Can someone
please post this? Right now, for the sake of simplicity ive got this example:

main (int argc, char **argv[])
{
char *instr;
instr = argv[1];
printf("%s\n", instr);
}

It does its job just fine, but its got that obnoxious warning, why?

itsme86 07-29-2005 12:22 PM

Code:

main (int argc, char **argv[])
The second parameter in main() is a NULL-terminated array of strings. Hence, you can declare it as char *argv[] or char **argv, but not char **argv[] like you have it.

lyd 07-29-2005 02:28 PM

But the song remains the same, actually, it just got worse, your
suggestion compiles into a segmentation fault;
i sure would like to know <b>why</b>.

<code>
char *foo
</code>
i believe this is a memory address reservation with room
for one addresses as long as one byte
<code>
char foo[]
</code>
this is an illegal declaration for a good reason, finite
memory, plus there are other processes in that memory
that have to leave eachother alone if the system is to keep
running. side note, declaring main(char foo[]) will compile
where as main() { char foo[]; } will not.

ive tried about 4 of the 8 possible *foo[]/*var || foo[]/var
assignments, and so far i cant make any of the main (char foo[])s
do anything other than cause a segmentation fault, if i find something
ill ammend my post

itsme86 07-29-2005 02:43 PM

I'm not sure why it's not working for you:
Code:

itsme@itsme:~/C$ cat arg1.c
#include <stdio.h>

int main (int argc, char *argv[])
{
char *instr;
instr = argv[1];
printf("%s\n", instr);
return 0;
}
itsme@itsme:~/C$ gcc -Wall arg1.c -o arg1
itsme@itsme:~/C$ ./arg1 "this is arg1"
this is arg1
itsme@itsme:~/C$



All times are GMT -5. The time now is 06:45 PM.