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

Discussion in 'C Programming' started by Juggernaut, Apr 8, 2005.

  1. Juggernaut

    Juggernaut Guest

    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");

    }

    }
     
    Juggernaut, Apr 8, 2005
    #1
    1. Advertising

  2. Juggernaut

    Jason Guest

    Juggernaut wrote:
    > 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
     
    Jason, Apr 8, 2005
    #2
    1. Advertising

  3. Juggernaut

    tigervamp Guest

    Juggernaut wrote:
    > 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
     
    tigervamp, Apr 8, 2005
    #3
  4. Juggernaut

    Juggernaut Guest

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

    Thanks to you both, that solved it :)

    "Juggernaut" <> wrote in message
    news:Z6j5e.110499$...
    > 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");
    >
    > }
    >
    > }
    >
    >
    >
     
    Juggernaut, Apr 8, 2005
    #4
  5. Juggernaut

    Joe Wright Guest

    Juggernaut wrote:
    > 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).

    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Apr 8, 2005
    #5
  6. Juggernaut

    Mattjack40 Guest

    A question about a subject in the "C Unleashed" book

    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
     
    Mattjack40, Apr 8, 2005
    #6
  7. Re: A question about a subject in the "C Unleashed" book

    Mattjack40 wrote:
    > 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;

    --
    Peter
     
    Peter Nilsson, Apr 8, 2005
    #7
  8. Juggernaut wrote:
    > 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.
     
    Martin Ambuhl, Apr 8, 2005
    #8
  9. Re: A question about a subject in the "C Unleashed" book

    On Thu, 07 Apr 2005 18:34:49 -0700, Peter Nilsson wrote:

    > Mattjack40 wrote:
    >> 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 };


    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
     
    Lawrence Kirby, Apr 8, 2005
    #9
  10. Juggernaut

    Mattjack40 Guest

    Re: A question about a subject in the "C Unleashed" book

    Thanks Lawrence,


    BTW, You guys have written a great book.


    Matt



    Lawrence Kirby wrote:
    > On Thu, 07 Apr 2005 18:34:49 -0700, Peter Nilsson wrote:
    >
    > > Mattjack40 wrote:
    > >> 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 };

    >
    > 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
     
    Mattjack40, Apr 9, 2005
    #10
  11. Juggernaut

    CBFalconer Guest

    Re: A question about a subject in the "C Unleashed" book

    Mattjack40 wrote:
    >
    > 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.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
     
    CBFalconer, Apr 9, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?dHJlbGxvdzQyMg==?=

    HELP! HELP! HELP! Opening Web Application Project Error

    =?Utf-8?B?dHJlbGxvdzQyMg==?=, Feb 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    776
    =?Utf-8?B?dHJlbGxvdzQyMg==?=
    Feb 20, 2004
  2. Harvey
    Replies:
    0
    Views:
    721
    Harvey
    Jul 16, 2004
  3. Harvey
    Replies:
    1
    Views:
    856
    Daniel
    Jul 16, 2004
  4. =?Utf-8?B?S2ltb24gSWZhbnRpZGlz?=

    HELP - HELP - HELP

    =?Utf-8?B?S2ltb24gSWZhbnRpZGlz?=, Mar 9, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    688
    Eliyahu Goldin
    Mar 9, 2006
  5. Dawn Minnis
    Replies:
    4
    Views:
    1,537
    Dawn Minnis
    Feb 20, 2005
Loading...

Share This Page