how to return a string in c

2

I'm doing a function to capture a string of alphanumeric and numeric characters I've seen several ways to be able to take the string back but I do not get it

I leave my code if someone could tell me what I'm doing wrong.

#include <stdio.h>
#include <windows.h>
#include <stdlib.h>

void gotoxy(int x,int y){
      HANDLE hcon;
      hcon = GetStdHandle(STD_OUTPUT_HANDLE);
      COORD dwPos;
      dwPos.X = x;
      dwPos.Y= y;
      SetConsoleCursorPosition(hcon,dwPos);
}
char*  CaptureString(int Lin_Ini ,int Col_Ini , int Min_Len , int Max_Len , int Format ) {

    int i=0;
    int Len_Captura;
    char captura;
    int prueba;
    char cadena[10];

    if (Format == 1){    
        while(TRUE){
            int i=0;    
            gotoxy(Lin_Ini ,Col_Ini);
            captura = getch();
            sprintf(&cadena[strlen(cadena)] , "%c" , captura);

            if (captura >=48 && captura <=57 || captura == 8 || captura == 13){        
                if (captura == 8){    
                    memset(&cadena[strlen(cadena)], 0x00 , 1);    
                }    
                printf("%s" ,cadena);

                if (captura == 13){    
                    return cadena;    
                }
            }
        }
    }    
    else if (Format == 2 ){    
        while(TRUE){
            int i=0;    
            gotoxy(Lin_Ini ,Col_Ini);
            captura = getch();

            if (captura >=97 && captura <=122 || captura >=48 && captura <=57 || captura == 8){

                sprintf(&cadena[strlen(cadena)] , "%c" , captura);
                printf("%s" ,cadena);

                if (captura == 8){    
                    memset(&cadena[strlen(cadena)], 0x00 , 1);    
                }
            }
        }
    }
}

int main(){
    char Captura_fin;
    printf("Bienvenido al programa 3 captura de una cadena con formato \n");    
    Captura_fin = CaptureString(12,15,4,12,1);    
    printf("la cadena escrita es ");
    puts (&Captura_fin);    
    return 0;
}
    
asked by Alejo_nii 19.07.2017 в 16:56
source

1 answer

4
char*  CaptureString(int Lin_Ini ,int Col_Ini , int Min_Len , int Max_Len , int Format )
{
  char cadena[10];

  // ...

  return cadena;
}

The variable cadena , as declared, is using the program's stack. When leaving the function that reserve is lost and the variable simply ceases to exist.

For the chain to survive the function has to be stored in the heap , that is, you have to use dynamic memory:

char*  CaptureString(int Lin_Ini ,int Col_Ini , int Min_Len , int Max_Len , int Format )
{
  char* cadena = malloc(10);

  // ...

  return cadena;
}

Of course, in that case you should not forget to call free when the returned variable is no longer needed.

To avoid this nuisance, the functions of the standard usually receive the strings as a reference. This allows the person who consumes the function to decide where they want to locate the variable:

void CaptureString(char* cadena, /* ... */)
{
  // ...
}

By the way, notice that not all the paths of the CaptureString function make return . that can bring you problems and is a practice to avoid.

    
answered by 19.07.2017 в 17:10