0

I have the following program I am trying to run but surely, due to my lack of good knowledge, my program crashes runtime:

#include <stdio.h>
#include "ptref.h"

mystruct_t    *FRSt = NULL;

int main(int argc, char* argv[])
{
    char ct[2] = {0, 1, '\0'};
    char dd[2] = {0, 1, '\0'};


    populate_contents(FRSt, 2, "FRES", ct, dd);


    return 0;

}

HEADER

/*
 * ptref.h
 *
 */

#ifndef PTREF_H_
#define PTREF_H_

typedef struct mystruct
{
    char* ct[2];    //
    char* dd[2];  // = "0\0";
    char* name[]; // = "1\0";
} mystruct_t;

extern mystruct_t p;

void populate_contents(mystruct_t* mystruct_var, int arrSize, char* name[], char* dd[], char* ct[])
{
    /* Initialise arrays */
    int i;

    i = 0;
    strncpy(mystruct_var->name, name, sizeof(name));


    for (i = 0; i < arrSize; i++)
    {
        mystruct_var->dd[i] = dd[i];
        mystruct_var->ct[i] = ct[i];
    }

    return;
}




#endif /* PTREF_H_ */

Because I am going to implement this in a real-time computer, I am not sure if using malloc will cause me any trouble. However, I have got a feeling that because I have not used malloc for my mystruct_var pointer, I am having trouble, or may be it is my moronic code. In any way, further education and advise will be highly appreciated.

P.S. I have looked into the other relevant post but my problem is quite different. So, I posed a new question.

ha9u63a7
  • 6,233
  • 16
  • 73
  • 108

1 Answers1

1

Firstly, in main() char ct[2] = {0, 1, '\0'}; this particular array initialization is incorrect as you have defined array size as 2 and initializing 3 array elements.

In function populate_contents(FRSt, 2, "FRES", ct, dd);, the third argument is a character string which corresponding called function argument should be a char array as char name[] or char pointer as char *name. It should not be as you defined name as array of pointers char *name[]. Same thing goes for arguments passed ct & dd, they should be just char pointers in the callee function as there type is char *.

Also your structure mystruct_t declared is incorrect by the way looking at your usage of member elements.

As said by Grijesh, sizeof(name) is what you don't want as name is a pointer which could be 4 or 8 Bytes, so make use of strlen() to get the length of the string you received.

Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
  • My answer was similar to you hence I deleted (as you came first). Add about `sizeof(name)` also. it is sizeof pointer not length OP want length. +1 – Grijesh Chauhan Feb 18 '14 at 18:27
  • @SunEric Incorrect structure, do you mean the char* declarations? I have updated them to `char* ct, char* dd, and char* name`, after you mentioned, but I still receive a SIGSEGV segmentation fault when I tried to execute `strncpy(mystruct_var' '->name, name, strlen(name))'. I don't know what is wrong! – ha9u63a7 Feb 18 '14 at 18:47
  • @hagubear I suggest you don't use `strncpy()` to copy the strings as it doesn't guarantee the NULL termination, [Read this for more details](http://stackoverflow.com/questions/14065391/strncpy-leading-to-segmentation-fault/14067026#14067026). Reason for SIGSEGV could be you are writing into memory where you haven't assigned `FRst` struct. – Sunil Bojanapally Feb 18 '14 at 19:05
  • @hagubear memory area is not secured. – BLUEPIXY Feb 18 '14 at 19:07
  • @SunEric Sorry, but could you be more specific? I mean I don't undstand what to use. I cannot do a strncpy() then should I use dereference pointer `*mystruct_var->name=*name` then? – ha9u63a7 Feb 18 '14 at 19:29