Pointer incompatible type assignment to character.

Discussion in 'C Programming' started by gk245, Apr 20, 2006.

  1. gk245

    gk245 Guest

    I have something like this:

    #include <stdio.h>

    main ()
    {
    struct line
    {
    char write[20];
    char read[20];

    struct line *next;
    };

    struct line n1;

    n1.write= "concepts";

    }

    However, if i try to compile it, i get a compiler error saying that
    "incompatible types in assignment". Whats strange is that if i set
    write as an integer, i don't get such a error and it compiles. Does
    something special need to be done with character strings and pointers?

    Thanks.
     
    gk245, Apr 20, 2006
    #1
    1. Advertising

  2. gk245

    Ian Collins Guest

    gk245 wrote:
    > I have something like this:
    >
    > #include <stdio.h>
    >
    > main ()
    > {
    > struct line
    > {
    > char write[20];
    > char read[20];
    >
    > struct line *next;
    > };
    >
    > struct line n1;
    >
    > n1.write= "concepts";
    >
    > }
    >
    > However, if i try to compile it, i get a compiler error saying that
    > "incompatible types in assignment". Whats strange is that if i set
    > write as an integer, i don't get such a error and it compiles. Does
    > something special need to be done with character strings and pointers?
    >

    They are different. You have declared write as an array of 20 char and
    you are attempting to assign a pointer to const char to it.

    I think you are confused regarding accessing an array through a pointer
    and assigning to an array. You have to copy the string literal into the
    array.

    --
    Ian Collins.
     
    Ian Collins, Apr 20, 2006
    #2
    1. Advertising

  3. On Wed, 19 Apr 2006 19:01:27 -0400, gk245 <> wrote:
    > I have something like this:
    >
    > #include <stdio.h>
    >
    > main ()
    > {
    > struct line
    > {
    > char write[20];
    > char read[20];
    >
    > struct line *next;
    > };
    >
    > struct line n1;
    >
    > n1.write= "concepts";
    >
    > }


    This is probably the usual confusion around arrays and pointers. You
    cannot treat an array "as if" it was a pointer in the left part of an
    assignment. Array names "decay" to pointers (to their first element)
    when they are in the right part of an assignment, but the reverse is not
    true.

    You will have to use strncpy() or strlcpy() to copy the data from your
    constant string into the array member of the structure, i.e. with:

    size_t len;

    len = sizeof(n1.write);
    strncpy(n1.write, "concepts", len - 1);
    n1.write[len - 1] = '\0';

    or

    strlcpy(n1.write, "concepts", sizeof(n1.write));
     
    Giorgos Keramidas, Apr 20, 2006
    #3
  4. gk245 <> writes:
    > I have something like this:
    >
    > #include <stdio.h>
    >
    > main ()
    > {
    > struct line
    > {
    > char write[20];
    > char read[20];
    >
    > struct line *next;
    > };
    >
    > struct line n1;
    >
    > n1.write= "concepts";
    >
    > }
    >
    > However, if i try to compile it, i get a compiler error saying that
    > "incompatible types in assignment". Whats strange is that if i set
    > write as an integer, i don't get such a error and it compiles. Does
    > something special need to be done with character strings and pointers?


    <http://www.c-faq.com/>. Read all of section 6, "Arrays and Pointers".

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Apr 20, 2006
    #4
  5. gk245

    gk245 Guest

    Keith Thompson formulated on Wednesday :
    > gk245 <> writes:
    >> I have something like this:
    >>
    >> #include <stdio.h>
    >>
    >> main ()
    >> {
    >> struct line
    >> {
    >> char write[20];
    >> char read[20];
    >>
    >> struct line *next;
    >> };
    >>
    >> struct line n1;
    >>
    >> n1.write= "concepts";
    >>
    >> }
    >>
    >> However, if i try to compile it, i get a compiler error saying that
    >> "incompatible types in assignment". Whats strange is that if i set
    >> write as an integer, i don't get such a error and it compiles. Does
    >> something special need to be done with character strings and pointers?

    >
    > <http://www.c-faq.com/>. Read all of section 6, "Arrays and Pointers".


    Thx for the link and the help guys. ^^
     
    gk245, Apr 20, 2006
    #5
  6. gk245

    Chad Guest

    > This is probably the usual confusion around arrays and pointers. You
    > cannot treat an array "as if" it was a pointer in the left part of an
    > assignment. Array names "decay" to pointers (to their first element)
    > when they are in the right part of an assignment, but the reverse is not
    > true.
    >


    Okay, I'm confused. Then how come something like this works.

    include <stdio.h>
    #define BUF 6

    int main(void) {
    char arr[BUF] = "la";

    printf("the value is: %s\n", arr);

    return 0;
    }


    Chad

    $gcc -Wall arr.c -o arr
    $./arr
    the value is: la


    But something like this:

    include <stdio.h>
    #define BUF 6

    int main(void) {
    int arr[BUF] = "la";

    printf("the value is: %s\n", arr);

    return 0;
    }


    produces
    $gcc -Wall arr.c -o arr
    arr.c: In function `main':
    arr.c:5: error: invalid initializer
    arr.c:7: warning: char format, different type arg (arg 2)
     
    Chad, Apr 20, 2006
    #6
  7. gk245

    Chad Guest

    > This is probably the usual confusion around arrays and pointers. You
    > cannot treat an array "as if" it was a pointer in the left part of an
    > assignment. Array names "decay" to pointers (to their first element)
    > when they are in the right part of an assignment, but the reverse is not
    > true.
    >


    Okay, I'm confused. Then how come something like this works.


    #include <stdio.h>
    #define BUF 6

    int main(void) {
    char arr[BUF] = "la";

    printf("the value is: %s\n", arr);

    return 0;

    }


    $gcc -Wall arr.c -o arr
    $./arr
    the value is: la

    But something like this:

    #include <stdio.h>
    #define BUF 6

    int main(void) {
    int arr[BUF] = "la";

    printf("the value is: %s\n", arr);

    return 0;

    }

    produces
    $gcc -Wall arr.c -o arr
    arr.c: In function `main':
    arr.c:5: error: invalid initializer
    arr.c:7: warning: char format, different type arg (arg 2)

    Chad
     
    Chad, Apr 20, 2006
    #7
  8. gk245

    Guest

    Integer arrays cannot be assigned strings, you can assign one character
    at a time to the array--

    like arr[0]='l';
    arr[1]='a';

    Because when characters are assigned to integers, theri ascii value
    gets transferred, but the ascii value of strings cannot be calculated.
     
    , Apr 20, 2006
    #8
  9. gk245

    Old Wolf Guest

    Chad wrote:
    >> Array names "decay" to pointers (to their first element) when they
    >> are in the right part of an assignment, but the reverse is not true.

    >
    > Okay, I'm confused. Then how come something like this works.
    >
    > char arr[BUF] = "la";


    This is not an assignment; it is an initialization. Some
    programming languages use a different symbol for initialization
    than they do for assignment. But C uses the equals sign for both.

    In the initialization case, it means that "la" is an initializer for
    arr. The C standard defines specifically that arrays of char can
    be initialized from string literals.

    > int arr[BUF] = "la";


    Other arrays can only be initialized by an initializer list, eg:
    int arr[BUF] = { 1, 2 };

    The case of initializing from a string literal is only for arrays of
    char.
     
    Old Wolf, Apr 20, 2006
    #9
  10. Groovy hepcat Ian Collins was jivin' on Thu, 20 Apr 2006 11:06:02
    +1200 in comp.lang.c.
    Re: Pointer incompatible type assignment to character.'s a cool scene!
    Dig it!

    >gk245 wrote:
    >> struct line
    >> {
    >> char write[20];
    >> char read[20];
    >> struct line *next;
    >> };
    >>
    >> struct line n1;
    >>
    >> n1.write= "concepts";


    [Snipage.]

    >They are different. You have declared write as an array of 20 char and
    >you are attempting to assign a pointer to const char to it.


    No, he's trying to assign a pointer to char to it. There is no const
    qualification on a string literal. It's not modifiable, but not const
    qualified either.

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
     
    Peter Shaggy Haywood, Apr 22, 2006
    #10
  11. Groovy hepcat was jivin' on 19 Apr 2006
    20:54:38 -0700 in comp.lang.c.
    Re: Pointer incompatible type assignment to character.'s a cool scene!
    Dig it!

    >Integer arrays cannot be assigned strings, you can assign one character
    >at a time to the array--
    >
    >like arr[0]='l';
    > arr[1]='a';
    >
    >Because when characters are assigned to integers, theri ascii value
    >gets transferred, but the ascii value of strings cannot be calculated.


    What on Earth are you talking about? Without seeing any quoted text,
    it's hard to know what you're trying to say; especially when what
    you've written is unclear in itself.
    Now, as to what you said, it's bunk. An array of char is an "integer
    array", since char is an integer type. And you certainly can
    initialise an array of char with a string literal. And EBCDIC based
    implementations don't use ASCII. Nor do implementations that use other
    non-ASCII character sets. And just what is "the ascii value of
    strings" supposed to mean? Do you mean the values of the characters in
    a string? Why would you want to calculate their values? You can,
    though, should the need arise.

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
     
    Peter Shaggy Haywood, Apr 22, 2006
    #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. zmbdcqnrdfetnws

    incompatible pointer assignment ?

    zmbdcqnrdfetnws, Dec 5, 2003, in forum: C Programming
    Replies:
    8
    Views:
    577
    Martin Ambuhl
    Dec 6, 2003
  2. Brian Stubblefield

    initialization from incompatible pointer type

    Brian Stubblefield, May 25, 2004, in forum: C Programming
    Replies:
    3
    Views:
    15,233
    Brian Stubblefield
    May 27, 2004
  3. Olaf \El Blanco\

    assignment from incompatible pointer type

    Olaf \El Blanco\, Jun 10, 2006, in forum: C Programming
    Replies:
    1
    Views:
    522
    Olaf \El Blanco\
    Jun 10, 2006
  4. Bart Vandewoestyne

    assignment from incompatible pointer type

    Bart Vandewoestyne, Apr 27, 2010, in forum: C Programming
    Replies:
    2
    Views:
    2,267
    Eric Sosman
    Apr 27, 2010
  5. Prathamesh Kulkarni

    incompatible pointer assignment

    Prathamesh Kulkarni, Dec 10, 2012, in forum: C Programming
    Replies:
    7
    Views:
    327
    Keith Thompson
    Dec 10, 2012
Loading...

Share This Page