Structs, strtok, segmentation fault

Posted by FILIaS on Stack Overflow See other posts from Stack Overflow or by FILIaS
Published on 2010-05-14T13:12:45Z Indexed on 2010/05/14 13:24 UTC
Read the original article Hit count: 342

I'm trying to make a programme with structs and files.The following is just a part of my code(it;s not all). What i'm trying to do is: ask the user to write his command. eg. delete John eg. enter John James 5000 ipad purchase. The problem is that I want to split the command in order to save its 'args' for a struct element. That's why i used strtok. BUT I'm facing another problem in who to 'put' these on the struct.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100

char command[1500]; 

struct catalogue                
{
        char short_name[50];
        char surname[50];
        signed int amount;
        char description[1000];
}*catalog[MAX]; 

int main ( int argc, char *argv[] )
{
    int i,n;
    char choice[3];

    printf(">sort1: Print savings sorted by surname\n");
    printf(">sort2: Print savings sorted by amount\n");
    printf(">search+name:Print savings of each name searched\n");
    printf(">delete+full_name+amount: Erase saving\n");
    printf(">enter+full_name+amount+description: Enter saving \n");
    printf(">quit:  Update + EXIT program.\n");

    printf("Choose your selection:\n>");
    gets(command);                     //it save the whole command

    /*in choice it;s saved only the first 2 letters(needed for menu choice again)*/
    strncpy(choice,command,2);      
    choice[2]='\0';                   

char** args = (char**)malloc(strlen(command)*sizeof(char*));
memset(args, 0, sizeof(char*)*strlen(command));

char* curToken = strtok(command, " \t");
for (n = 0; curToken != NULL; ++n)
{
    args[n] = strdup(curToken);
    curToken = strtok(NULL, " \t");

    *catalog[n]->short_name=*args[1];
            *catalog[n]->surname=args[2];
    catalog[n]->amount=atoi(args[3]);
    *catalog[n]->description=args[4];
}

return 0;

}

I get a warning (warning: assignment makes integer from pointer without a cast) for the lines:

*catalog[n]->short_name=*args[1];
*catalog[n]->surname=args[2];
*catalog[n]->description=args[4];

As a result, after running the program i get a Segmentation Fault...

Any help? Any ideas?

© Stack Overflow or respective owner

Related posts about c

    Related posts about strtok