0

I'm not new to C, but this doesn't make sense in my mind. in my char encrypt function, I get this warning:

crypt.h: In function ‘encrypt’:
crypt.h:32:9: warning: returning ‘char *’ from a function with return type ‘char’ makes integer from pointer without a cast [-Wint-conversion]
   32 |   return(encrypted_string);
      |         ^

Please note: This is supposed to return char not char*

I have fixed this seemingly by changing it to char *encrypt. But that doesn't make sense. Can somebody explain how and why this works this way? The code seems to work, but clarity would be nice.

Here's my code:

char encrypt(char string[])
{
  // allocating new buffer for encrypted string
  // note: normal string
  char encrypted_string[strlen(string)];

  // comparing string to cipher
  for(int i = 0; i < strlen(string); i++)
  {
    for(int j = 0; j < strlen(CHARSET); j++)
    {
      if(string[i] == CHARSET[j])
      {
        encrypted_string[i] = CIPHER[j];
        break;
      }
    }
  }
  return(encrypted_string);// returns pointer?
}
R-Rothrock
  • 303
  • 2
  • 14
  • 1
    Besides the error, you are returning a local array which goes out of scope once the function ends. – 001 Nov 10 '22 at 14:09
  • `return(encrypted_string);// returns pointer?` - yes, but to a memory address which is not necessarily valid once the function returns (i.e., outside the scope of the function stack). –  Nov 10 '22 at 14:10
  • 1
    Re "*But that doesn't make sense.*", Quite the opposite. Using `char` makes no sense. You don't want to return a single `char`. /// But as mentioned above, it's not enough to switch to `char *`. You can't return a pointer to an automatic ("local") variable. – ikegami Nov 10 '22 at 14:10
  • You should allocate `char encrypted_string[strlen(string)]` outside the function, and then pass it (`char encrypted_string[]`) to the function. No need for that function to return anything explicitly. –  Nov 10 '22 at 14:12
  • @ikegami I suspected that it was better to return a pointer, but are you also saying that there is no proper way to return a string? Please answer this an an actual post, so that it can be voted. EDIT: are you saying that I should be augmenting a pointer as an arguments for the function? – R-Rothrock Nov 10 '22 at 14:12
  • Re "*but are you also saying that there is no proper way to return a string?*", That's ridiculous. – ikegami Nov 10 '22 at 14:14
  • @R-Rothrock It is maybe helpful to consider, that C language doean't really _have_ strings. It has string literals, and it has functions which process bytes until first 0-byte they encounter, but these are far cry from strings on just about any other language. – hyde Nov 10 '22 at 14:15
  • I know that @hyde. What I meant was is there no proper way to return `char[]` not `char*`. – R-Rothrock Nov 10 '22 at 14:16
  • @R-Rothrock The problem is more like, there are about 5 different ways to return a string from a function. So you must choose one. `strncat` _or_ `strdup` might be good models to imitate. – hyde Nov 10 '22 at 14:17
  • @hyde Can you please have that as an answer so that it can be accepted? – R-Rothrock Nov 10 '22 at 14:18
  • @R-Rothrock In most contexts, `char[]` and `char*` are the same thing (used as pointer to char). – hyde Nov 10 '22 at 14:19
  • I can write an answer in a few hours, if nobody else beats me to it (so anybody, go for it). – hyde Nov 10 '22 at 14:20
  • @R-Rothrock Note: in the end, `encrypted_string[]` is not a _string_ as it lacks a certain _null character_. – chux - Reinstate Monica Nov 10 '22 at 17:26

1 Answers1

1
  • char encrypted_string[strlen(string)]; is an array, that decays into a char* whenever used in an expression.
  • Your function returns char, an integer type.
  • Thus: "returning ‘char *’ from a function with return type ‘char’"

Related post: "Pointer from integer/integer from pointer without a cast" issues


Please note: This is supposed to return char not char*

That really doesn't make any sense.

I have fixed this seemingly by changing it to char *encrypt. But that doesn't make sense.

Well true, but maybe not for the reason you thought...You can't return a pointer to a local array inside a function, as explained by any decent beginner-level learning material (and thousands of other SO posts).

You can fix your program by using caller allocation instead and return the result through a parameter. Or return a char* to a dynamically allocated string, but in that case mind memory leaks.

Also possible bugs here: for(int j = 0; i < strlen(CHARSET); i++). Should probably be j not i.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • @R-Rothrock I just now also noticed that the inner loop looks fishy. Should be `j` not `i`, yeah? – Lundin Nov 10 '22 at 14:28
  • Yes, yes it should. :/ I didn't really look at that, I just needed clarity with warning. I'll edit it. – R-Rothrock Nov 10 '22 at 14:30