incompatible types in assignment

B

Brian Stubblefield

Dear clc members,

I am new to C and am posting several messages concerning a large C
program that I am debugging.

I am encountering a "incompatible types in assignment" warning for the
following code during the compile stage:

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

int main(void)
{

/* removed code */

short int n = 10;
char key[n][6];
short int i = { 0};

for(i = 1; i <= n; i++)
{
key = ' '; /* incompatible types in assignment */
{

/* removed code */
}

If there is a similar posting that provides the answer(s), please
point me in the direction. I really do not know what look for in my
search.

Any help is greatly appreciated.

Thank you,

Brian
 
K

Kevin C.

Brian Stubblefield said:
key = ' '; /* incompatible types in assignment */


key is a 2D array. Since you are only referencing one dimension, what you
end up getting is a char pointer to the underlying array, and hence this
statement is trying to assign a char constant to a char pointer. You can
either use double quotes to change the constant to a C-style string or you
can explicitly reference key[0] which is what I assume you intended.
 
E

Eric Sosman

Brian said:
Dear clc members,

I am new to C and am posting several messages concerning a large C
program that I am debugging.

I am encountering a "incompatible types in assignment" warning for the
following code during the compile stage:

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

int main(void)
{

/* removed code */

short int n = 10;
char key[n][6];

If this gets through the compiler, it's either a very
new ("C99") implementation or you're using some compiler-
specific extensions to Standard C. Prior to the C99 edition
of the Standard, array dimensions could only be constants,
and you would have to write `char key[10][6];' instead.
short int i = { 0};

The initialization is pointless, because you're about
to assign new values to `i' in the immediately succeeding
`for' loop.
for(i = 1; i <= n; i++)

The limits on `i' are suspect. In C, an array of ten
elements has indices [0], [1], and so on through [9], but
*not* [10]. If this is not yet second nature to you, I
suggest that you are not yet equipped to debug "a large
C program" and you are likely to create more trouble than
you solve. Seriously. Think "novice sword-swallower on
karaoke night."
{
key = ' '; /* incompatible types in assignment */


What is `key'? It is an array of ten elements. What
are those elements? Each is an array of six `char'. In
particular, `key' (for legal values of `i') is an array
of six `char'.

What is the thing on the right-hand side? A single
`char'. (Pedants would say it's an `int' and they'd be right,
but at your present stage of development I think you'd find
the knowledge a distraction. "It's a `char', and damn the
torpedoes.)

Now: Is an array of six somethings the same as a single
something? No, so the attempted assignment makes no sense.
The stuff on the r.h.s. must have the same type as the thing
on the l.h.s., or at least be convertible to it.

As in your other recent difficulty, you're now faced with
trying to figure out what the code's author was trying to
accomplish, after which you need to write a correct expression
of that intention. The original author has said "Gzorgflash"
and it's up to you to discover that he really meant "O for a
Muse of fire!" There is no way any expert or quasi-expert out
here in Usenetland can do this for you given only the original
"Gzorgflash" -- and if the program is as large as you say (and
as badly written as the snippets suggest), I doubt anyone will
undertake the task for free.
{

/* removed code */
}

If there is a similar posting that provides the answer(s), please
point me in the direction. I really do not know what look for in my
search.

Any help is greatly appreciated.

My sincere advice is to hire someone who's C knowledge
exceeds yours. At this point it seems you are a beginner, and
you've been dropped into the deep end without a life jacket.
You are not going to make much progress unaided, and the amount
of aid you require is probably greater than a channel like
Usenet can provide. "Get professional help."
 
B

Barry Schwarz

Brian Stubblefield said:
key = ' '; /* incompatible types in assignment */


key is a 2D array. Since you are only referencing one dimension, what you
end up getting is a char pointer to the underlying array, and hence this


No, key is not a pointer. key is the i-th element of key. You
snipped it from the OP's code but key is an array of n arrays of 6
char. Therefore, key is the i-th array of 6 char. It is an array.
statement is trying to assign a char constant to a char pointer. You can
either use double quotes to change the constant to a C-style string or you

An array is not a modifiable lvalue. Therefore it cannot appear on
the left of an assignment operator. Changing the character constant
to a string literal will not make the assignment legal.
can explicitly reference key[0] which is what I assume you intended.




<<Remove the del for email>>
 
M

Martin Ambuhl

Brian said:
Dear clc members,

I am new to C and am posting several messages concerning a large C
program that I am debugging.

I am encountering a "incompatible types in assignment" warning for the
following code during the compile stage:

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

At least 3 of the above are unused.
int main(void)
{

/* removed code */

short int n = 10;
char key[n][6];
short int i = { 0};

The use of 'short int' is probably a false economy.
for(i = 1; i <= n; i++)

The line above indicates a lack of understanding of subscripts in
C.
{
key = ' '; /* incompatible types in assignment */


The above indicates a confusion between scalar characters and an
array of char.
{

/* removed code */
}


Perhaps you meant something like:


#include <string.h>

int main(void)
{
size_t n = 10, i;
char key[n][6];

for (i = 0; i < n; i++)
strcpy(key, " ");

return 0;
}
 
B

Brian Stubblefield

Thank you all for your programming suggestions and advice. I have
been able to resolve the warning messages.
 
R

RoSsIaCrIiLoIA

Dear clc members,

I am new to C and am posting several messages concerning a large C
program that I am debugging.

I am encountering a "incompatible types in assignment" warning for the
following code during the compile stage:

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

int main(void)
{

/* removed code */

short int n = 10;
char key[n][6];
short int i = { 0};

what does it means
short int i = { 0 }
I have the mean only for
short int i[1]={0};
 
M

Michael Fyles

RoSsIaCrIiLoIA said:
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{

/* removed code */

short int n = 10;
char key[n][6];
short int i = { 0};

what does it means
short int i = { 0 }
I have the mean only for
short int i[1]={0};

For a scalar, the braces are optional. It means the same
thing as short int i = 0;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top