0

Why is my function not returning a value?

https://code.sololearn.com/c2aCps3Ye556/?ref=app Ive tried using pointers in my functions (call by reference) instead of calling by value but every time i try something out, it just doesnt work. Here in my code, ive tried returning a pointer from a function and then dereferencing it when calling it. Is it ok to do so? My program wont print anything and would actually generate a runtime error. But i cant seem to understand why that is the case. Kindly help🙏 Thanks!!

1st Nov 2018, 2:01 PM
Yusha Arif
Yusha Arif - avatar
4 Respostas
0
Here's what I found: 1) You accidentally closed the main function with an extra } 2) when you use strcat() the first argument should be a string variable 3) I don't know why &inp[0] was used, just inp would've made more sense 4) *bracket_by_len() was not needed because the return type is a char array
1st Nov 2018, 2:59 PM
jtrh
jtrh - avatar
+ 1
thanks alot jtrh and Bebida Roja That did indeed solve the problem. I was making the mistake of storing the string "word" in a constant which is not possible. @jtrh Thanks also for pointing out the other errors in my program. However, i do want to point out here that since my function returns a pointer, is it ok for me to simply write return temp; or do i have to create a seperate pointer for temp and then return it
1st Nov 2018, 3:48 PM
Yusha Arif
Yusha Arif - avatar
0
the first argument to strcat cannot be a constant, as the concatenation is stored in that address.
1st Nov 2018, 2:34 PM
Bebida Roja
Bebida Roja - avatar
0
This works if you wanna give special brackets to the end of the string with a certain length #include <stdio.h> #include <string.h> char *bracket_by_len ( char *word, int size) { char temp[20000];     if (size<5)     { strcpy(temp,"<<");         strcat(temp,word);         strcat(temp,">>");     }     else if (size>5 && size<10)     {         strcpy(temp,"(*");         strcat(temp,word);         strcat(temp,"*)");     }     else     {         strcpy(temp,"/+");         strcat(temp,word);         strcat(temp,"+/");     }     return temp ; } int main() {     char inp[30];    printf("Enter a word: ");    scanf("%s",inp);     printf("%s",bracket_by_len(inp,strlen(inp))); return 0; }
1st Nov 2018, 2:52 PM
jtrh
jtrh - avatar