Summary
I am trying to pass the text that user has put in the terminal and pass it to the function named printString(). I don't fully understand C but I think I has to do with the pointer not being in the heap. Any help would be appreacitaed!
#include <stdio.h>
void printString();
int main (int argc, char* argv[]) {
int commandAmount = 1;
while (commandAmount < argc) {
printString(commandAmount, &argv);
}
return 0;
}
void printString(int commandAmount, char* argv[]) {
printf("the word is %s," , argv[commandAmount]);
}
./shortExample example
Segmentation fault (core dumped)
CodePudding user response:
The prototype void printString();
does not match the actual implementation.
It should have been:
void printString(int commandAmount, char* argv[]);
You could also skip the prototype and just implement the function before main
. Your loop while (commandAmount < argc)
seems to not have any way to finish since you never increase commandAmount
. This can cause undefined behavior and with such, your program may crash or do just about anything.
I suggest making a for
-loop to fix that.
Example:
#include <stdio.h>
void printString(int commandAmount, char* argv[]) {
printf("the word is %s,", argv[commandAmount]);
}
int main(int argc, char* argv[]) {
for(int commandAmount = 1; commandAmount < argc; commandAmount) {
printString(commandAmount, argv);
}
}
or in the way you structured it:
#include <stdio.h>
void printString(int commandAmount, char* argv[]); // corrected
int main(int argc, char* argv[]) {
int commandAmount = 1;
while (commandAmount < argc) {
printString(commandAmount, argv);
commandAmount; // needed
}
}
void printString(int commandAmount, char* argv[]) {
printf("the word is %s,", argv[commandAmount]);
}
CodePudding user response:
For starters this function declaration
void printString();
does not provide a function prototype. So the compiler determines the type of the parameters of the function from the function call
printString(commandAmount, &argv);
However the expression &argv
used in this call
printString(commandAmount, &argv);
has the type char ***
due to the declaration of the identifier argv
int main (int argc, char* argv[]) {
^^^^^^^^^^^^
But the corresponding parameter in the definition of the function printString
has the type char **
due to adjusting by the compiler parameters having array types to pointers to array element type.
That is this function declaration
void printString(int commandAmount, char* argv[]) {
is adjusted by the compiler to
void printString(int commandAmount, char** argv) {
^^^^^^^^^^^
Thus there are incompatible types of the argument expression and of the parameter. As a result this call
printf("the word is %s," , argv[commandAmount]);
invokes undefined behavior.
Moreover this loop in main
int commandAmount = 1;
while (commandAmount < argc) {
printString(commandAmount, &argv);
}
in general is an infinite loop because the variable commandAmount
is not changed within the loop.
Firstly you should provide the function prototype before main
to make your program more safer
void printString(int commandAmount, char** argv);
and call the function like
printString(commandAmount, argv);
^^^^
Of course you need also to change the loop in main.
Pay attention to that as the value of the parameter commandAmount
is not outputted within the function then in fact it is redundant. You could pass to the function the pointer to the string itself. For example
#include <stdio.h>
void printString( const char *s );
int main( int argc, char* argv[] )
{
for ( int commandAmount = 1; commandAmount < argc; commandAmount )
{
printString( argv[commandAmount] );
}
putchar( '\n' );
return 0;
}
void printString( const char *s )
{
printf( "the word is %s, " , s );
}