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?

5
  • Possible duplicate of gcc compile error: cast specifies array type Commented Mar 21, 2016 at 8: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. Commented Mar 21, 2016 at 8:49
  • 1
    Stop writing casts (particulary if you have no idea what you are doing) Commented Mar 21, 2016 at 8: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. Commented Mar 21, 2016 at 8:54
  • MAX_LINE_LENGTH is a strange name for the count of how many lines you have. Commented Mar 21, 2016 at 8:55

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

5 Comments

...and it will appear a new error asking what is int member... ;)
I guessed so. BTW you cannot solve OP problem with your solution. It will give you: error: assignment to expression with array type
But then you get error of pointer to local variable, don't you?
@matyah No, local variables resides on the stack, while malloc() allocates memory on the heap to nw.
@matyah So after copySentence() returns, *nw, which is on the heap, is not effected.
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.

Comments

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.