简体   繁体   中英

Failing to pass a string through a function in c

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 ?

As I mentioned in a comment you just need to allocate space on the heap using malloc so that it is not destroyed when the function ends (pops off the stack). Just make sure to use free() at the end of your main.

#include <stdio.h>
#include <string.h>
#include <malloc.h>

#define StrLen 100

char * LenCorr(char * str);

int main(void)
{
  char t1[StrLen] = "Hello3\0";
  printf("Input : %s\n", t1);  
  
  char *t3 = LenCorr(t1);
  printf("LenCorr p: %p\n", t3); 
  printf("LenCorr s: %s\n", t3); 
  free(t3);
}

char * LenCorr(char * str)
{
  char si[StrLen];
  char st[StrLen] = "iiiiiiiiiiiiiiiiiii\0";
  strncpy(si,str, strlen(str));
  int ln = strlen(si);
  int ct = 8 - ln;
  
  if (ln < 8) { strncat(si, st, ct); }
  char * output = malloc(strlen(si) * sizeof(char)); //heap-allocation
  strncpy(output, si, strlen(si));
  
  printf("Debug output: %s\n", output);
  printf("Debug output pointer: %p\n", output);
  
  return output;
}

A char array lifetime is confined to the scope where it's declared, as soon as the function execution ends it's no longer legal to access the local array.

That is exactly what happens here, output will be pointing to an invalid memory location when it's returned, later accessing this memory location as you do in main amounts to undefined behavior.

The idiomatic way to do this is to pass a pointer to the buffer, where you want to store the string, as an argument of the function:

#include <stdio.h>
#include <string.h>

#define StrLen 100

void LenCorr(char *str, char* output); // passing a pointer to the destination bufffer
void main(void)
{
    char t1[StrLen] = "Hello3";

    char t3[StrLen]; // destination buffer

    printf("Input : %s\n", t1);  
    
    LenCorr(t1, t3); // pass destination buffer as argument

    printf("LenCorr p: %p\n", t3); 
    printf("LenCorr s: %s\n", t3);
}
void LenCorr(char * str, char *output)
{
    char si[StrLen]; 
    char su[StrLen];
    char st[StrLen] = "iiiiiiiiiiiiiiiiiii";
    
    strcpy(si,str); 

    int ln = strlen(si);
    int ct = 8 - ln;
    
    if (ln < 8) {strncat(si, st, ct);}

    strcpy(su, si); 

    strcpy(output, su); // copy string to its final destination
    
    printf("Debug output: %s\n", output);
    printf("Debug output pointer: %p\n", output);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM