1

So I have this piece of code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hw09-header.h"

struct student
{
    char* name;
    char* course;
};

int main(int argc, char* argv[])
{
    int i = 0, init_size=10,x,z;
    char *value = "go";
    int key, count=0;
    char* del = ","; /*Uses comma sign as delimiter*/
    char *token=NULL;
    char *temp_stor;
    struct student *array;
    struct student *temp;

    if(argc != 2)
    {
        printf("  usage:  program_name positive_integern");
        printf("example:  ./example-hw09  123n");
        exit(1);
    }

    /**************  begin REQUIRED  **************/
    /*  put before logic.  DO NOT PUT IN A LOOP */
    key = atoi(argv[1]);
    initialize(key);
    /**************   end REQUIRED   **************/

    /*  example loop  */

    array=malloc((init_size)*sizeof(int));

    while(strcmp(value, "stop") != 0)
    {
        value = getString();
        token = strtok(value, del);
        while (token !=NULL)
        {
            if(i%4==0)
            {
                init_size=init_size*2;
                temp = realloc(array,init_size*sizeof(int)) ;
                if(temp != NULL)
                {
                    array = temp;
                }
                else
                {
                    printf("unable to reallocaten");
                    exit(1);
                }
            }

            array[i].name=malloc(sizeof(struct student)*10);
            strcpy(array[i].name,token);
            printf("%s %dn",array[i].name,i);
            token = strtok( NULL, del );
            array[i].course=malloc(sizeof(struct student)*11);
            strcpy(array[i].course,token);
            printf("%s n",array[i].course);
            i=i+1;
            token = strtok( NULL, del );
            x=i;
            for(x=0; x<i; x++)
            {
                if(strcmp(array[x].name,token)==0)
                    printf("Duplicate found n");
            }
        }
    }
}

Now when I try to do strcmp, it always gives me a segmentation error and I'm not sure why.

I'm not supposed to use linked lists here, and I think I have everything done right upto here, for the next few parts I just need to compare and sort things and I keep getting that segmentation error.

And my array does have elements in it, I can print them all out just not compare them for some reason.

5
  • Are you sure that student's name has length only 9 symbols? Commented Apr 17, 2013 at 6:12
  • 1
    Please re-paste your code with tabs removed (configure your editor to use just spaces and re-indent). Also compile your code with warnings enabled (for gcc -Wall -Wextra) and see if you get any hints from compiler. Commented Apr 17, 2013 at 6:12
  • Yes, that's a given in part of the assignment that the name won't be longer than 9 letters. Commented Apr 17, 2013 at 6:15
  • you're right token is NULL...... Commented Apr 17, 2013 at 6:22
  • Your memory allocations seem to be almost random chosen - do you understand what's going on when you do things like: array=malloc((init_size)*sizeof(int)); or array[i].name=malloc(sizeof(struct student)*10);? Because the expressions being used to calculate the allocation size don't seem to have much to do with the types of the pointers you're assigning the allocation to. Commented Apr 17, 2013 at 6:26

2 Answers 2

2

Partial answer, pointing out things which make no sense. You should try to understand why, so you can fix them. But SO is not the right site to explain how something like malloc works.


    array[i].name=malloc(sizeof(struct student)*10);
    strcpy(array[i].name,token);

You allocate space for 10 student structs, then you copy string to it. That makes no sense. Since name is char* you should have malloc(<maximum size of string with terminating 0 included>).


    array=malloc((init_size)*sizeof(int));

then later

    array[i].name= .....

You allocate array as array of ints (indicated by sizeof(int)), but then you use items it as if they were structs.


Then suggestion: Every time you have strcpy(dst, src), replace it with this:

snprintf(dst, <how much space is allocated at dst>, "%s", src);

That will avoid buffer overflow, and it will also force you to think how much space you have allocated for dst (if you do not know, then there's your first problem to solve and undderstand).

Sign up to request clarification or add additional context in comments.

3 Comments

While it's good to point out specific places he made a mistake, I think the general advice people in intro courses need is that they shouldn't write lines of code without thinking about what they mean. It is pretty obvious OP does not know what malloc does or at least wrote the line with other intentions. If OP cannot read his code, line-by-line, and actually explain what it should do then there's no point in asking "why does strcmp throw a segfault".
@roliu Yeah. I clarified the answer a bit. I wrote in the hope of getting OP to go and learn these things so he can fix them.
also did not need this: snprintf(dst, <how much space is allocated at dst>, "%s", src); since it wasn't covered in class but the other stuff like how malloc and realloc really works helped a lot, again thank you guys
1

Since, it's apparent (also you said that) token is null.

if(strcmp(array[x].name,token)==0)

It's illegal to pass NULL parameter to strcmp.
If string compare functions is called with NULL as one parameter the process will get a SIGSEGV,
because the functions are dereferencing NULL pointers.

1 Comment

ya the strtok(NULL,del), messed me up, thought that was passing me the data =/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.