0

I have set a structure type:

typedef struct {
    char *snt[MAX_LINE_LENGTH];
} sentence;

And this line is getting a cast specifies array type error:

sentence copySentence(sentence *source) {
    sentence nw;
    nw.snt = (char *[])source->snt; //Here is the error
    return nw;
}

What is the best fix for this code line and what is the problem?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
maty ah
  • 13
  • 4
  • Possible duplicate of [gcc compile error: cast specifies array type](http://stackoverflow.com/questions/3427843/gcc-compile-error-cast-specifies-array-type) – ste-fu Mar 21 '16 at 08:44
  • change `char *sentence[MAX_LINE_LENGTH];` to `char **sentence;` and `malloc` it according to `MAX_LINE_LENGTH`. Then you'll not need to cast anything. – LPs Mar 21 '16 at 08:49
  • 1
    Stop writing casts (particulary if you have no idea what you are doing) – M.M Mar 21 '16 at 08:52
  • You can write `nw = *source;` instead of that line. Or replace the whole function with `return *source;` . This will do a shallow copy. If you want a 'deep copy' then you need to explain more about how you allocate memory for your sentences. – M.M Mar 21 '16 at 08:54
  • `MAX_LINE_LENGTH` is a strange name for the count of how many lines you have. – M.M Mar 21 '16 at 08:55

2 Answers2

1

Both nw.snt and source->snt are arrays of pointer. To "deep copy" the whole array, you may want to use memmove(nw.snt, source->snt, MAX_LINE_LENGTH * sizeof (char *));.

Also, people usually prefer passing a pointer to a struct than pass that struct directly to reduce the cost of argument passing. In this case, you can

sentence *copySentence(sentence *source) {
    sentence *nw;
    nw = malloc(sizeof (struct sentence));
    memmove(nw.snt, source->snt, MAX_LINE_LENGTH * sizeof (char *));
    return nw;
}
nalzok
  • 14,965
  • 21
  • 72
  • 139
  • ...and it will appear a new error asking what is int member... ;) – LPs Mar 21 '16 at 10:53
  • I guessed so. BTW you cannot solve OP problem with your solution. It will give you: `error: assignment to expression with array type` – LPs Mar 21 '16 at 11:08
  • But then you get error of pointer to local variable, don't you? – maty ah Mar 21 '16 at 13:51
  • @matyah No, local variables resides on the **stack**, while `malloc()` allocates memory on the **heap** to `nw`. – nalzok Mar 21 '16 at 14:02
  • @matyah So after `copySentence()` returns, `*nw`, which is on the heap, is not effected. – nalzok Mar 21 '16 at 14:03
0

You are declaring snt as an array of pointers to charactes. You probably mean it t be an array of charactes, or a pointer to an array of characters:

char snt[MAX_LINE_LENGTH];    // array of characters to hold your sentence
char *snt;                    // pointer to array of characters

When you assign an element to a compatible element, there is no need for a cast and casting is here considered harmful because you prevent the compiler from giving you warnings. Note that you do not copy the characters, you only make two structs point to the same sentence.

I leave fixing this to you, as an excercise.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41