-1

I am having difficulty passing strings between functions in C. Here is the most simplified parts of my code that contain the errors: First, the function prototype for transpose, which performs a certain unary operation on strings. I've heard we declare strings with char *str, so this is correct?

char *transpose(*char);
//[Error] expected primary-expression before 'char'

Here is the use of the transpose function in main(). I indicated which lines produce errors with the following comment.

int main() {

    char *input;
    scanf("%s",&input);

    char *result;
    result = transpose(input);
    //[Error] 'transpose' cannot be used as a function

    printf("%s",result);
    return 0;

Finally is the function definition for transpose:

char *transpose(char *text) {
    char *T = text;
    return T;
}
mdnestor
  • 137
  • 2
  • 9
  • `char *T = text;` goes out of scope as soon as you return from that function, I believe. – David Vereb Oct 02 '18 at 17:35
  • @DavidVereb it does, but its pointee doesn't. – Quentin Oct 02 '18 at 17:36
  • 1
    `char *str` is a _pointer_. Where does it point to? – ForceBru Oct 02 '18 at 17:36
  • 4
    The code as posted doesn't generate the error messages you mentioned. Please update your question with a [mcve] that others can compile that reproduces the issue. – dbush Oct 02 '18 at 17:38
  • @Quentin ah yes, whoops. Now when I compile this code myself it compiles just fine when removing the & in front of input in the `scanf()` function, so I'm not sure where OP is having an issue. – David Vereb Oct 02 '18 at 17:39
  • `input` does not point to any buffer, anyways. – Christian Gibbons Oct 02 '18 at 17:39
  • "*heard we declare strings with char *str, so this is correct*" unfortunately not. `char *` just defines a pointer. No `char`-array here, which is the least you need to have string. – alk Oct 02 '18 at 17:41
  • `char *input;` -> `char input[100];`. And `scanf("%s",&input);` -> `scanf("%s", input);` Read the chapters dealing with sitring and with pointers in your C text book. – Jabberwocky Oct 02 '18 at 17:42
  • The question is related to this one https://stackoverflow.com/q/37549594/694576 if not a duplicate to it. – alk Oct 02 '18 at 17:42
  • Ok I updated the new errors. I'm receiving many different answers for this problem, all I'm trying to do is create an identify function for strings. – mdnestor Oct 02 '18 at 17:43
  • 3
    If you are just learning C, I strongly suggest that you avoid using `scanf`. It will cause unnecessary confusion. – William Pursell Oct 02 '18 at 17:45
  • When you function definition is starting with `char *transpose(char *text)`, that is taking a pointer to `char`, a `char*`, then why you make the prototype to be `char *transpose(*char)`? What to you expected `*char` to be, this poor lonely cripple? ;) – alk Oct 02 '18 at 17:45
  • The function prototype should be a copy of the function definition, with a semicolon added on the end. So `char *transpose(char *text);` is the prototype that should be in the first code snippet. – user3386109 Oct 02 '18 at 17:59

1 Answers1

1

There are few issues with your codes, firstly here

char *input; /* pointer variable should be initialized with valid memory */
scanf("%s",&input); /* this causes compiler warning, but seems you ignored ? */

input doesn't have valid memory & scanning a uninitialized pointer may cause crash. So either you can create input as char array like

char input[20]; 
scanf("%s",input); /* & is not required as input name itself address */

Or allocate memory dynamically for input like below

char *input = malloc(SIZE); /* define the SIZE value */
scanf("%s",input); 

Sample Code

char *transpose(char *text) {
    char *T = text;
    return T;
}
int main(void) {
        char input[20];/* size of array you can define it, I just took 20 char */
        scanf("%s",input);
        char *result = transpose(input);
        printf("%s",result);
        return 0;
}

As others suggested in comments its better to use fgets() instead of scanf(). For e.g

char input[20];
fgets(input,sizeof(input),stdin);
input[strcspn(input, "\n")] = 0; /* to remove the trailing \n if copied, use strcspn() */
Achal
  • 11,821
  • 2
  • 15
  • 37