I'm learning c and performing some exercise with strings, functions and pointers. The exercise is to take a string into a function, make an elaboration and give a result out. The function 'LenCorr' add 'i' letters to the end of input string to achieve 8 chars lenght.
My code is:
#include <stdio.h>
#include <string.h>
#define StrLen 100
char * LenCorr(char * str);
void main(void)
{
char t1[StrLen] = "Hello3"; // this is the input string
printf("Input : %s\n", t1);
char *t3 = LenCorr(t1); // pass through the 'LenCorr' function to t3 pointer to char array
printf("LenCorr p: %p\n", t3); // then I print the pointer ...
printf("LenCorr s: %s\n", t3); // ... but the content is empty
}
char * LenCorr(char * str)
{
char si[StrLen]; // I create various strings
char su[StrLen];
char st[StrLen] = "iiiiiiiiiiiiiiiiiii";
strcpy(si,str); // I have to copy the 'str' input to the local string
// here I perform some elaborations
int ln = strlen(si);
int ct = 8 - ln;
if (ln < 8) {strncat(si, st, ct);}
// ... and the final result is correct into 'si' string
strcpy(su,si); // that I copy into a 'su' string (another)
char * output = su;
printf("Debug output: %s\n", output); // here I see the result of string elaboration
printf("Debug output pointer: %p\n", output); // here I get a pointer of the string elaboration
return output;
}
But at the end the function returns the proper pointer value but the value is 0. Here the console output:
Input : Hello3
Debug output: Hello3hh
Debug output ptr: 0x7fff4cf17c30
LenCorr p: 0x7fff4cf17c30
LenCorr s:
Why ?