0

I am trying to figure out why I am getting a segmentation fault when trying to copy the arguments from one array of strings to another but just leaving out the >

int main(){
     char *args[] = {"sort", "myshell.c", ">", "2"};
     int size = 4*sizeof(args)/sizeof(args);
     char *temp[size];
     int i;

     printf("SIZE: %d\n", size);
     for(i = 0; i < size; ++i){
            if(strcmp(args[i], ">") > 0 || strcmp(args[i], ">") < 0 ){
                strcpy(temp[i],args[i]);

            }
            printf("arg: %s\n", temp[i]);
     }
  • `4*sizeof(args)/sizeof(args);` what is the purpose of multiplying and dividing by the same number? – babon Dec 09 '17 at 06:05
  • Yeah I just noticed. Sorry about that, silly mistake, still learning –  Dec 09 '17 at 06:14

3 Answers3

0
strcpy(temp[i],args[i]); 

This is undefined behavior. strcpy tries to access an indeterminate memory location resulting in undefined behavior.

You need to allocate memory to temp[i] or simply use

temp[i]=strdup(args[i])

to copy the string to temp[i].

Also

int size = 4*sizeof(args)/sizeof(args);

would be

int size = sizeof(args)/sizeof(*args);

If you know the length of the array to be 4 then what's the use of doing all those calculation. You need to calculate it not put it like that.

Example code:-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
     char *args[] = {"sort", "myshell.c", ">", "2"};
     size_t size = sizeof(args)/sizeof(*args);
     char *temp[size];
     size_t len = 0;
     printf("SIZE: %zu\n", size);
     for(size_t i = 0; i < size; ++i){
            if(strcmp(args[i], ">") != 0 ){
                temp[len++] = args[i];
            }
     }
     for(size_t i = 0; i < len; i++){
        printf("arg[%zu]: %s\n", i+1, temp[i]);
     }
     return 0;
 }

Note that your comparison should be much more simpler as mentioned above.

What does strdup do?

Internally it allocates memory and then copies the target string using strcpy. This is not part of the standard library and implemented by POSIX. For more detail look at this answer .

Community
  • 1
  • 1
user2736738
  • 30,591
  • 5
  • 42
  • 56
  • I fixed it by using that function you mention, strdup(), but I am not sure why? how does it work ? –  Dec 09 '17 at 06:18
  • @CodeDexter.: I have mentioned reference and a general idea. Hope it helps. Check the answer – user2736738 Dec 09 '17 at 06:48
0

Your code crashes here at strcpy(temp[i],args[i]); as temp[i] has uninitialised values. You need allocate memory and store the address in temp[i] before calling strcpy

Karthick
  • 1,010
  • 1
  • 8
  • 24
0
  1. You are trying to copy data (strings in this case) into the memory pointed by temp[i] - but this pointer is uninitialized and no memory is allocated. You should allocate memory:

    temp[i] = (char )malloc(sizeof(char)(strlen(args[i])+1))

  2. To leave out the ">", you can just test for != 0. You will need a secondary index for the copied strings.

  3. What is the purpose of sizeof(args)/sizeof(args)? Try:

    int main()
    {
         char *args[] = {"sort", "myshell.c", ">", "2"};
         int size = sizeof(args)/sizeof(char *);
         char *temp[size];
         int i, j;
    
         printf("SIZE: %d\n", size);
         for (j = 0, i = 0; i < size; ++i)
         {
                if (strcmp(args[i], ">") != 0)
                {
                    temp[j] = (char *)malloc(sizeof(char)*(strlen(args[i])+1));
                    strcpy(temp[j],args[i]);
                    printf("arg: %s\n", temp[j]);
                    j++;
                }
         }
    }
    
Yaniv Shaked
  • 698
  • 6
  • 12