-2

I am learning pointer in c i have written a small program , but i am getting segmentaion fault i dont know where i am having the issue please let me know the issue with the code , it is an array of pointers to string , which is in a pointer to structure .

# include <stdio.h>
#include <stdlib.h>
 # include <string.h>
char   *sum(char **sol) ;
 char  *summer_sum(char*** solcs) ;
int main()
{
char* datum ="teststring";
sum(&datum);
}


char *sum(char** sol)  
{
printf("\n   value is : %s",*sol);
summer_sum(&sol);
return "1" ; 
}

char *summer_sum(char***  solcs)  
{
int i=0;
typedef struct
{
char *asg[40];
}nlist;
nlist *n1;
for( i=0 ; i<= 38 ;i++)
{ 
n1->asg[i] = calloc(1,1*sizeof(*solcs));
strcpy(n1->asg[i],**solcs);
printf("\n %d value is : %s",i,n1->asg[i]);
} 

return NULL; 
}
  • any reason to use the return type as `char *` , when that is not at all used? Also, if you are working with dynamic memories, get used to with some dynamic memory debugger tool like [valgrind](http://valgrind.org/). – Sourav Ghosh Jan 02 '14 at 11:55
  • Pro tip: Don't overcomplicate things, it doesn't make you or your code any better. – Jite Jan 02 '14 at 12:03
  • Try to debug your code to see where and why the error occures? – user1781290 Jan 02 '14 at 12:12

4 Answers4

1

n1 is used uninitialized:

n1->asg[i] = calloc(1,1*sizeof(*solcs));

On the other hand, if you want to allocate enough space for the use of strcpy, you must use strlen instead of sizeof

And you don't need a double or triple pointer, your code simplified:

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

void sum(char *sol);
char *summer_sum(char *solcs);

int main(void)
{
    char *datum = "teststring";
    sum(datum);
}

void sum(char *sol)  
{
    printf("\n   value is : %s", sol);
    summer_sum(sol);
}

char *summer_sum(char *solcs)  
{
    int i = 0;
    size_t len;
    typedef struct {
        char *asg[40];
    } nlist;

    nlist *n1 = malloc(sizeof(*n1));
    if (n1 == NULL) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }
    len = strlen(solcs); /* No need to compute len on each iteration */
    for (i = 0; i <= 38; i++) { /* you are filling 39 and declared 40 */
        n1->asg[i] = calloc(1, len);
        /* Always check the result of (m/c/re)alloc */
        if (n1->asg[i] == NULL) {
            perror("malloc");
            exit(EXIT_FAILURE);
        }
        strcpy(n1->asg[i], solcs);
        printf("\n %d value is : %s", i, n1->asg[i]);
        /* Don't forget to free */
        free(n1->asg[i]);
    }
    free(n1);
    return NULL; 
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
0

You are using your pointer n1 uninitialized. Your program invokes undefined behavior. In such case you may get either expected or unexpected result.
Second, you are missing a closing brace } in function summer_sum.

haccks
  • 104,019
  • 25
  • 176
  • 264
0

Before using n1->... You will have allocate memory to n1 as well using calloc()

Digital_Reality
  • 4,488
  • 1
  • 29
  • 31
0

There are two problems which involves dynamic memory allocation in your code:

  1. n1 is not initialized, you should add a statement like n1 = malloc(sizeof(*n1)); before the for statement in summer_sum()
  2. you did not allocate enough space for each asg[i], you should allocate spaces for these asg[i] by n1->asg[i] = malloc(strlen(**solcs) + 1);
Lee Duhem
  • 14,695
  • 3
  • 29
  • 47