Problem with char pointer function C, can anyone explain?

Can someone help me find errors in this C function?

char* f(int i) { int i; char buffer[20]; switch ( i ) { 1: strcpy( buffer, "string1"); 2: strcpy( buffer, "string2"); 3: strcpy( buffer, "string3"); default: strcpy(buffer, "defaultstring"); } return buffer; } 

I think this is related to type conversion. My compiler gives a warning that "declaration int i shadow parameter".

+4
source share
11 answers
  char* f(int i) { int i; 

Error 1: local parameter "i" obscures function argument "i"

  char buffer[20]; switch ( i ) { 

Error 2: you are using a local variable i that is not initialized.

  1: strcpy( buffer, "string1"); 2: strcpy( buffer, "string2"); 3: strcpy( buffer, "string3"); default: strcpy(buffer, "defaultstring"); } return buffer; 

Error 3: you return a pointer to an element in a local array, this pointer is not valid when returning a function. "buffer" is out of scope.

+8
source

Well, firstly, you are missing the "case" declaration in the switch statement, as well as the breaks at the end of the statement. Try changing the code:

  char* f(int i) { int i; char buffer[20]; switch ( i ) { case 1: strcpy( buffer, "string1"); break; case 2: strcpy( buffer, "string2"); break; case 3: strcpy( buffer, "string3"); break; default: strcpy(buffer, "defaultstring"); break; } return buffer; } 

In addition, you override your parameter i local variable named i . Therefore remove this:

  char* f(int i) { char buffer[20]; switch ( i ) { case 1: strcpy( buffer, "string1"); break; case 2: strcpy( buffer, "string2"); break; case 3: strcpy( buffer, "string3"); break; default: strcpy(buffer, "defaultstring"); break; } return buffer; } 

Finally, you return a char pointer that points to a local statically declared character array called buffer . buffer drops out of scope as soon as a pointer returns to its first element (which is what your return statement does). This means that you get a pointer to a variable that no longer exists. To fix this, you can pass a pointer to a buffer in a function, for example:

  void f(char *buffer, int i) { switch ( i ) { case 1: strcpy( buffer, "string1"); break; case 2: strcpy( buffer, "string2"); break; case 3: strcpy( buffer, "string3"); break; default: strcpy(buffer, "defaultstring"); break; } } 

This latest version should work if you make sure buffer is long enough to accept a string. If you really want to make it safe, take the length of the buffer and check that against the length of the string that you are copying.

+6
source

A couple of fixes applied here

  const char* f(int i) { switch ( i ) { case 1: return "string1"; case 2: return "string2"; case 3: return "string3"; } return "defaultstring"; } 
+2
source

There are 3 big problems:

  • int i declared as a function parameter, and a local automatic variable. Remove the int i; operator int i; in line 2.
  • The case keyword must execute each value in a switch statement (for example, case 1: instead of 1: .
  • You are returning a pointer to an automatic variable. Variable char buffer[20]; is pushed onto the stack and is no longer valid after the function returns. Instead, you should allocate memory on the heap and return a pointer to it.

Fixed function using strdup instead of strcpy :

 char * f(int i) { switch (i) { case 1: return strdup("string1"); case 2: return strdup("string2"); case 3: return strdup("string3"); default: return strdup("defaultstring"); } } 
+2
source

You have parameter i , and you declare local variable i .

By declaring a local variable with the same name as the parameter, you can no longer access both. you must change the name of the local variable or the name of the parameter.

+1
source

You declare a local variable i that obscures the parameter i function.

+1
source

Shadows the parameter. I am the argument of the function, then declared inside the function. In addition, you cannot declare an array on the stack (char buffer [20]) and then return it. It (the buffer) leaves the scope immediately after the function exits.

+1
source

The problem is what she tells you. From the very beginning you have:

 char* f(int i) { int i; 

The second line defines a variable called i - which “obscures” (hides) i , which was passed as a parameter. When you execute your switch on the value of i , you do this on the (uninitialized) value of the local variable instead of the parameter you received.

You also define buffer local to f , so the return is an issue - it no longer exists at the time of his return. Given that what you put in it is any of several string literals, you'd better return the string literals immediately:

 char const *f(int i) { switch (i) { case 1: return "string 1"; case 2: return "string 2"; case 3: return "string 3"; default: return "default string"; } } 

Alternatively, you can use an array of strings:

 char const *f(int i) { char *strings[] = {"", "string 1", "string 2", "string 3"}; if (i>0 && i<3) return strings[i]; return "default string"; } 
+1
source

You are returning a local variable that can end only with tears.

+1
source

There are several issues here.

  • You update me on line 2. I am already a parameter to the function. You do not need to update it.

  • Your syntax for the switch statement is incorrect. The correct syntax is something like this: case 1: /* do stuff */ break; case 2: /* do other stuff */ break; case 1: /* do stuff */ break; case 2: /* do other stuff */ break;

  • You are trying to return a pointer to a buffer allocated by the stack. This will cause subtle problems, as there is no guarantee that the memory will not be lost after the function returns. What is the purpose of strcpys? If you just want to return these lines, just return these lines!

Here is what I would do:

 char* f(int i) { char buffer[20]; switch ( i ) { case 1: return "string1"; case 2: return "string2"; case 3: return "string3"; default: return "defaultstring"; } return buffer; } 
+1
source
  • You do not need a local int i if you have the int i provided to you.
  • Index

    1: blabla;

you will need

 case 1: blabla; break; 

(for 1: 2: and 3: but not for the default value)

3. I am not sure about this (I haven’t done C for a long time), but I think that after the function is completed, the buffer will be empty. So you have to pass the char array (and size) and then change it. In this case, you do not need to return anything.

+1
source

Source: https://habr.com/ru/post/1306241/


All Articles