Home > Back-end >  How do parameters work in C - run a function with a string as a param
How do parameters work in C - run a function with a string as a param

Time:11-15

I am very new to C, so please forgive my ignorance.

I am trying to run this program:

int main()
{
    const char *str = "EWEWEWEEW";

    for (int i = 0; i < strlen(str);   i)
    {
        char ch = str[i];
        
        if (strlen(str) != 10) {
            return 0;
        }
    }
        
    return 1;
}

I wanted it so, just like in JS, you can run the program with the string included as a param:

Main would be like this int main(str) and then I would run the program like this:

main("EWEWEWEEW") so it returns either 0 or 1.

How can I achieve this in C?

Thanks

CodePudding user response:

I think I understand what you are asking. You want a program that will return 0 or 1 depending on whether the target string "EWEWEWEEW" is the argument passed to main(). Continuing from my comment above related to main().

What I understand from the last comment is you just want to test whether a string provided as an argument is the target string. You can do that trivially with strcmp() for the string comparison. You can write your code to return 0 if the argument is provided but not "EWEWEWEEW", return 1 if the argument is "EWEWEWEEW" or return some other value (say 2) if no argument was provided, e.g.

#include <stdio.h>
#include <string.h>

#define TARGET "EWEWEWEEW"

int main (int argc, char **argv) {
  
  if (argc > 1) {
    return strcmp (argv[1], TARGET) == 0;
  }
  
  return 2;
}

strcmp() returns -1 if argv[1] sorts before TARGET, 0 if the strings are equal and 1 if TARGET sorts before argv[1]. By returning the result of the equality strcmp (argv[1], TARGET) == 0 you force a return of 1 (true) if the strings match or 0 if they don't and you don't have to worry about the positive/negative aspect of the strcmp() return.

If you attempt the comparison with strlen() all that tells you is the length of the string. If that is what you want, then use that instead. Understand though, using strlen() that "EWEWEWEEW" and "WEEWEWEWE" will return the same thing.

Compile With Full Warnings Enabled -- Every Time

Always compile with warnings enabled, and do not accept code until it compiles without warning. To enable warnings add -Wall -Wextra -pedantic to your gcc/clang compile string (also consider adding -Wshadow to warn on shadowed variables). For VS (cl.exe on windows), use /W3. All other compilers will have similar options. Read and understand each warning -- then go fix it.

Adding the c11 standard to the compile string, and output the executable named checkstring I used:

$ gcc -Wall -Wextra -pedantic -Wshadow -std=c11 -Ofast -o bin/checkstring checkstring.c

For a DEBUG build, change -Ofast to -g to generate a symbol table for the gdb debugger to read.

(I have a bin subdirectory in my source code directory so I can compile and output an executable in a separate directory - makes clean up easier)

Example Use/Output

$ ./bin/checkstring EWEWEWEEW
$ echo $?
1

(note: quotes around your string argument are only needed if your string contains whitespace to prevent word-splitting by the shell)

or

$ ./bin/checkstring bananas
$ echo $?
0

and finally

$ ./bin/checkstring
$ echo $?
2

Let me know if that is what you are after, if not I'm happy to help further.

CodePudding user response:

... I just need to understand how to pass a string as a param

Well, simply take a look at how it's done by the standard string functions.

Example: https://man7.org/linux/man-pages/man3/strlen.3.html says:

size_t strlen(const char *s);

It kind of tells it all. To pass a string, you pass a char pointer, i.e. char*. In other words, in C you don't actually pass the text string as a value. Instead you pass a pointer to the text string.

The const is saying that the string will/can not be changed by the function. If the function is going to modify the string, just remove the const

So you can do:

int foo(const char *s)
{
    if (strlen(s) == 10) return 0;
    return 1;
}

and call it like

int a = foo("abcdef");
int b = foo("1234567890");

const char* str = "Hello world";
int c = foo(str);

char x[] = "More text";
int d = foo(x);

Notice how foo("abcdef"); looks as-if you pass a text string. But that's not what happens. The compiler will place the "abcdef" somewhere in memory and then pass a pointer to that memory to the function.

Final note:

Notice that the length of a string is not the same as the number of characters used for storing the string. This is because C strings require an extra character (a '\0' character) for indicating "end-of-string". So

"abc" has length 3 but is stored using 4 characters, i.e. 'a', 'b', 'c', '\0'

so

strlen("abc") gives 3
sizeof("abc") gives 4
  •  Tags:  
  • c
  • Related