0

I have the following code below in a function.

char * stringFiveds = strtok(stringFive[3], "ds");

When I compile i get the warning unused variable.

I don't plan on using srtingFiveds in this function. I want to use it in main(). I have two parts to this question.

  1. How to solve this warning if I don't want to use the stringFiveds in this function.
  2. How do I make it accessible in other functions so i can use it in other function i will create.

I'm new to this, can you please be as detailed as possible.

Bill Lynch
  • 72,481
  • 14
  • 116
  • 162
45aken
  • 23
  • 1
  • 2
  • 6

4 Answers4

2
  1. If you don't want to use stringFiveds in the function, you can delete that variable declaration because you don't need it.

  2. If you want to make it accessible to other functions, you can declare it as a global variable, or pass it as an argument to your other functions.

So instead of

char * stringFiveds = strtok(stringFive[3], "ds");

you can have just

strtok(stringFive[3], "ds");

If you want to declare stringFiveds as a global variable, just declare it outside of a function:

#include <stdio.h>

char * stringFiveds;  // declare outside of function

void foo() {
    // you can access stringFiveds here
}

int main() {
    // you can also access stringFiveds here   
}

If you want to pass stringFiveds as a function argument:

#include <stdio.h>

  // declare outside of function

void foo(char * stringFiveds) {

}

int main() {
    char * stringFiveds;
    foo(stringFiveds);

}
jh314
  • 24,533
  • 14
  • 58
  • 79
1

If you do not plan to use a variable in a function, do not make a variable. If you make a variable simply for the side effects of calling a function, you could drop the assignment: C lets you call a function that returns a value as if it were a "procedure" - i.e. a void function.

Another alternative is declaring the variable in the global scope. If you choose this route, consider making the variable static to keep it within the scope of its translation unit. You could also make the variable global, but that is usually not a good option, because it exposes your data beyond the bounds where it is useful.

If you plan to pass it to another function, that would count as "using the variable", resolving the "unused variable" warning.

Note: in case you are curious on how to silence this warning anyway, a common trick is to cast your variable to void.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 675,664
  • 71
  • 998
  • 1,399
0

Well, you could do the gross thing and make it a global variable (i.e. declare is outside of your function).

But that really isn't a great solution. That breaks data encapsulation by giving everything in your .c file access to that variable, whether they need to know about it or not.

A better solution would be to pass in to your functions as a parameter where you need it.

For example:

function foo(char* stringFiveDs) { // use stringFiveDs here }

Or, you could pass in stringFive and declare stringFiveDs in the functions that you need it. So something like:

function bar(const char* stringFive) {
     char* stringFiveDs = strtok(stringFive[3], "ds");
     ...
}
Terhands
  • 179
  • 1
  • 4
0

sample code

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

char **func(const char *str, const char *delim){
    char *strwk = strdup(str);
    char **strs = malloc(sizeof(char*));
    int count = 0;
    char *p;
    for(p=strtok(strwk, delim); p != NULL ; p=strtok(NULL, delim)){
        strs[count++] = strdup(p);//cutting string copy into 
        strs = realloc(strs, (count+1)*sizeof(char*));//+1 : Also, reserved for NULL
    }
    strs[count] = NULL;//end mark
    free(strwk);
    return strs;
}

void func_d(char **ss){
    char **sp = ss;

    while(*sp)
        free(*sp++);

    free(ss);
}

int main(void){
    char *stringFive[5] = {
        "", "16X16", "1,2,3", "123d45s678", "last,,end"
    };
    char **stringFiveds = func(stringFive[3], "ds");
    int i;
    for(i=0; stringFiveds[i] != NULL; ++i)
        printf("%s\n", stringFiveds[i]);
    func_d(stringFiveds);
    return 0;
}
BLUEPIXY
  • 38,201
  • 6
  • 29
  • 68