Help: "Assignment makes integer from pointer without a cast"

J

Juggernaut

Im working on an assignment in c (which im not very familiar with at all,
since I prefer java).
And im loosing my hair on this because I've already gotten stuck at the very
beginning, at the simplest thing (atleast it is in java), which is creating
a 2 dimensional array and filling it.

I get this error message when I try to compile it: with gcc" warning:
assignment makes integer from pointer without a cast".

Now I can't continue with my assignment (wich is huge) until I can come past
this (what I would think for anyone else would be a small problem) problem.
I've been reading about pointers and arrays now for the last hour and I just
don't get what Im doing wrong.

Can anyone help me on this so I can move on?

Here's the code:

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

#define NUM_THREADS 5

#define SIZE 10

int main(int argc, char *argv[])
{
char array[SIZE] [SIZE];
int i, j;



for( i=0; i<IS_SIZE; i++){
for( j=0; j<IS_SIZE; j++){
array[j] = "B";
printf("%d", array[j]);

}
printf("\n");

}

}
 
J

Jason

Juggernaut said:
Im working on an assignment in c (which im not very familiar with at all,
since I prefer java).
And im loosing my hair on this because I've already gotten stuck at the very
beginning, at the simplest thing (atleast it is in java), which is creating
a 2 dimensional array and filling it.

I get this error message when I try to compile it: with gcc" warning:
assignment makes integer from pointer without a cast".
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 5
#define SIZE 10

int main(int argc, char *argv[])
{
char array[SIZE] [SIZE];
int i, j;
for( i=0; i<IS_SIZE; i++){
for( j=0; j<IS_SIZE; j++){

IS_SIZE? Should this be simply SIZE?
array[j] = "B";


Try 'B' instead.
printf("%d", array[j]);


Did you want to print out the char value?
printf("%c", array[j]) works a lot better if that's what you wanted.

-Jason
 
T

tigervamp

Juggernaut said:
Im working on an assignment in c (which im not very familiar with at all,
since I prefer java).
And im loosing my hair on this because I've already gotten stuck at the very
beginning, at the simplest thing (atleast it is in java), which is creating
a 2 dimensional array and filling it.

I get this error message when I try to compile it: with gcc" warning:
assignment makes integer from pointer without a cast".

Now I can't continue with my assignment (wich is huge) until I can come past
this (what I would think for anyone else would be a small problem) problem.
I've been reading about pointers and arrays now for the last hour and I just
don't get what Im doing wrong.

Can anyone help me on this so I can move on?

Here's the code:

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

Above header not needed in this example.
#define NUM_THREADS 5

This isn't used either.

(Following code reformatted to increase readability)
#define SIZE 10

int main(int argc, char *argv[])
{
char array[SIZE] [SIZE];
int i, j;

for(i = 0; i < IS_SIZE; i++){

You haven't defined the symbol IS_SIZE, I assume this is supposed to be
SIZE.
for(j = 0; j < IS_SIZE; j++){

See previous comment.
array[j] = "B";


Here is the heart of your problem. In C, "B" is a string literal
consisting of the character 'B' followed by the nul character '\0' and
yields the address of the beginning of the string when evaluated, a
type of char* which is incompatible with your array. Character
constants in C are surrounded by single quotes:

array[j] = 'B';
printf("%d", array[j]);
}
printf("\n");
}
}


Rob Gamble
 
J

Joe Wright

Juggernaut said:
Im working on an assignment in c (which im not very familiar with at all,
since I prefer java).
And im loosing my hair on this because I've already gotten stuck at the very
beginning, at the simplest thing (atleast it is in java), which is creating
a 2 dimensional array and filling it.

I get this error message when I try to compile it: with gcc" warning:
assignment makes integer from pointer without a cast".

Now I can't continue with my assignment (wich is huge) until I can come past
this (what I would think for anyone else would be a small problem) problem.
I've been reading about pointers and arrays now for the last hour and I just
don't get what Im doing wrong.

Can anyone help me on this so I can move on?

Here's the code:

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

#define NUM_THREADS 5

#define SIZE 10

int main(int argc, char *argv[])
{
char array[SIZE] [SIZE];
int i, j;



for( i=0; i<IS_SIZE; i++){
for( j=0; j<IS_SIZE; j++){
array[j] = "B";
printf("%d", array[j]);

}
printf("\n");

}

}


I don't see IS_SIZE defined anywhere. But the main problem is ..

array[j] = "B";

... because array[j] has type char while "B" in this context is a
pointer to char.

Perhaps you mean 'B' (an int whose value will fit in a char).
 
M

Mattjack40

In page 37 the book says"

stuct like

stuc FOO foo
memset(&foo,0, sizeof foo);


is not guranteed to have the desired effect if the structure definition
of FOO contains floating point numbers or pointers. The book recommands
usigng {0} to do the job. (struct FOO foo={0})

My question is that we can initialize a stucture in the beginning of
the program using {0} but what about ,for example, middle of a program?
We can't just simple say (struct FOO foo={0}) in the middle of the
program. So how should we initialize it then?


Matt
 
P

Peter Nilsson

Mattjack40 said:
In page 37 the book says"

stuct like

stuc FOO foo
memset(&foo,0, sizeof foo);

is not guranteed to have the desired effect if the structure
definition of FOO contains floating point numbers or pointers.
The book recommands usigng {0} to do the job. (struct FOO foo={0})

My question is that we can initialize a stucture in the beginning
of the program using {0} but what about ,for example, middle of a
program?

static struct Foo foo_zero = { 0 };
struct Foo foo;
...
foo = foo_zero;
 
M

Martin Ambuhl

Juggernaut said:
Im working on an assignment in c (which im not very familiar with at all,
since I prefer java).

That, of course, is an absurd statement. You may be more familiar with
Java, but that is the reason for your preference. To claim that your
not being familiar with C is a *result* of your preference makes no
sense at all.
And im loosing my hair on this because I've already gotten stuck at the very
beginning, at the simplest thing (atleast it is in java), which is creating
a 2 dimensional array and filling it.

Your code, once corrected for your not having learned basic types in C,
is fine. If you made stupid type errors in Java, you wouldn't blame
your errors on Java, would you?
I get this error message when I try to compile it: with gcc" warning:
assignment makes integer from pointer without a cast".

For good reason.

[...]
char array[SIZE] [SIZE];
int i, j;
for( i=0; i<IS_SIZE; i++){
for( j=0; j<IS_SIZE; j++){
array[j] = "B";


"B" is an (anonymous) string, and you are trying to store its address in
a char. Use 'B' instead.
 
L

Lawrence Kirby

static struct Foo foo_zero = { 0 };

const is also appropriate here.
struct Foo foo;
...
foo = foo_zero;

You can also initialise a structure in the middle of a program. You can
initialise something anywhere you can define it. Pre-C99 that is at the
start of any block, with C99 that is almost anywhere in a function.

Lawrence
 
C

CBFalconer

Mattjack40 said:
Thanks Lawrence,

Better learn early - DON'T TOPPOST, please. Your answer belongs
after the material to which you are replying (or possibly
intermixed) after snipping out all material that is not germane to
your answer. The snipping is important too.
 

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

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top