-2

maybe it's a dumb question but I'm new in C. I'm trying to return a pointer from a function using malloc. I made an array with strtok. Here it's the code of the function from which I'm trying to return the pointer:

int *data(){
    
  int longi=0, *array=(int *) malloc(4 * sizeof(int));
  char buffer[1024];     
  char *aux;       
  printf("Enter if approved(A) or failed (F) separated by a comma \",\": \n");
  fgets(buffer,1023,stdin); 
  aux=strtok(buffer, ","); 
  while(aux)                 
  {
      array[longi]=aux; 
      longi++;                 
      aux=strtok(NULL, ","); 
  } 

  printf("%s", array[0]);
  return array;
}

And here is my main function:

int main(){
  int *arr=data();
  printf("%s",arr[0]); /*segmentation error */
  return 0;
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
Abner
  • 71
  • 1
  • 1
  • 6
  • 1
    `aux;` is a `char *`, yet `array[longi]` is an `int`. Why assign a pointer to an `int`? – chux - Reinstate Monica Aug 29 '20 at 23:26
  • 2
    Your `array` pointers point to some part of `buffer` which no longer exists after your function call. – jarmod Aug 29 '20 at 23:28
  • Are you looking at compiler warnings? Compile with `-Wall -Werror` please. The compiler pretty much is going to tell you all of the same things we are. If you compile everything cleanly and still segfault, then build with `-ggdb3` and use valgrind or gdb and it'll tell you what the problem is. – ggorlen Aug 30 '20 at 00:16
  • Better to use a char buffer `char* array = (char*)malloc(4 * sizeof(char));` and then `array[longi] = *aux`, of course you also must change your printf statement to `printf("%c\n", arr[0]);` or something like that. – Cyclonecode Aug 30 '20 at 00:20
  • Even better, [don't cast `malloc` in C programs](https://stackoverflow.com/a/605858/1322972) ; either in the OPs original code, or anywhere else on this page. Regardless, you have more urgent concerns, and there's enough here to strongly urge a [good book on C programming](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) as a reference point. – WhozCraig Aug 30 '20 at 00:31

2 Answers2

0

The segmentation fault occurs because you are trying to print part of your array as a string (%s). When using %d or %c (with char casting) it does not give that error. However, it still doesn't make sense, since you are trying to put a pointer to the beginning of a string inside an array of integers. I'd suggest allocating an array of characters instead of integers, and making sure you've got a single character inside aux, then adding it to the array. You also need to make sure you don't accept more than 4 different characters, otherwise you might still have a buffer overflow.

Liel Fridman
  • 1,019
  • 7
  • 11
  • Thanks for the help, you're right. Now I don't have any segmentation error, but I don't have any character at all. – Abner Aug 29 '20 at 23:51
  • You have a pointer to the string you get, so you can get the first character of it by using *aux – Liel Fridman Aug 29 '20 at 23:53
0

printf when flag is %s is trying to read string from allocated memory, you giving an integer so this why it gives a segfault replace array[longi]=aux; with array[longi]=atoi(aux); atoi will convert from string to integer, and for both printfs replace %s with %d

bari barix
  • 68
  • 4
  • I think the problem is that the the array entries points to the buffer which is no longer valid, since he only wants a char you could simply do `array[longi]=*aux;` instead of using something like `atoi()`. – Cyclonecode Aug 30 '20 at 00:05
  • i rushed, i thought he needed an integer so he just need a character you're right. – bari barix Aug 30 '20 at 00:36