conversion to non-scalar type requested error

Discussion in 'C Programming' started by venu reddy, Feb 19, 2007.

  1. venu reddy

    venu reddy Guest

    Hi all,
    I wrote an example code like this. I am getting error as " conversion
    to non-scalar type requested error". help me!!.


    #include<string.h>
    typedef struct
    {
    int val;
    char data[10];
    }xyz;

    main()
    {

    xyz a={10,"abcdefg"};
    int i=0;
    char data[20]={0};//={1,2,5,'c'};
    void *ptr=data;
    ptr=memcpy(data,&a,sizeof(xyz));

    printf("%s ptr=%u data= %u \n",data,ptr,data);
    printf("val=%u data=%s\n",((xyz)data).val,(xyz)data.data);
    printf("val=%u data=%s\n",(xyz*)ptr->val,(xyz*)ptr->data);

    }
    for the above progrmme i am getting errors like this (compiled in gcc)

    structCharmap.c: In function `main':
    structCharmap.c:22: error: conversion to non-scalar type requested
    structCharmap.c:22: error: request for member `data' in something not
    a structure or union
    structCharmap.c:23: warning: dereferencing `void *' pointer
    structCharmap.c:23: error: request for member `val' in something not a
    structure or union
    structCharmap.c:23: warning: dereferencing `void *' pointer
    structCharmap.c:23: error: request for member `data' in something not
    a structure or union

    My intention is, to typecast char* data to the structure and getting
    the data.

    ThanQ,
    venu
     
    venu reddy, Feb 19, 2007
    #1
    1. Advertising

  2. venu reddy wrote:
    > Hi all,
    > I wrote an example code like this. I am getting error as " conversion
    > to non-scalar type requested error". help me!!.
    >
    >
    > #include<string.h>
    > typedef struct
    > {
    > int val;
    > char data[10];
    > }xyz;
    >
    > main()
    > {
    >
    > xyz a={10,"abcdefg"};
    > int i=0;
    > char data[20]={0};//={1,2,5,'c'};
    > void *ptr=data;
    > ptr=memcpy(data,&a,sizeof(xyz));
    >
    > printf("%s ptr=%u data= %u \n",data,ptr,data);
    > printf("val=%u data=%s\n",((xyz)data).val,(xyz)data.data);
    > printf("val=%u data=%s\n",(xyz*)ptr->val,(xyz*)ptr->data);
    >
    > }
    > for the above progrmme i am getting errors like this (compiled in gcc)


    You should get even more diagnostics from gcc. You have your diagnostic
    level set far too low. You have no reason at all to think you can cast
    a char array to a struct. If you *must* play with type punning, almost
    always a bad idea, here is a better form of your code:

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

    typedef struct
    {
    int val;
    char data[10];
    } xyz;

    int main(void)
    {
    xyz a = { 10, "abcdefg" };
    char data[sizeof a];
    void *ptr = data;
    ptr = memcpy(data, &a, sizeof a);

    printf("%s ptr=%p data= %p\n", data, (void *) ptr, (void *) data);
    printf("val=%u data=%s\n", (*(xyz *) data).val,
    (*(xyz *) data).data);
    printf("val=%u data=%s\n", ((xyz *) ptr)->val,
    ((xyz *) ptr)->data);
    return 0;
    }
     
    Martin Ambuhl, Feb 19, 2007
    #2
    1. Advertising

  3. On Mon, 19 Feb 2007 05:41:09 -0500, Martin Ambuhl
    <> wrote:

    >venu reddy wrote:
    >> Hi all,
    >> I wrote an example code like this. I am getting error as " conversion
    >> to non-scalar type requested error". help me!!.
    >>
    >>
    >> #include<string.h>
    >> typedef struct
    >> {
    >> int val;
    >> char data[10];
    >> }xyz;
    >>
    >> main()
    >> {
    >>
    >> xyz a={10,"abcdefg"};
    >> int i=0;
    >> char data[20]={0};//={1,2,5,'c'};
    >> void *ptr=data;
    >> ptr=memcpy(data,&a,sizeof(xyz));
    >>
    >> printf("%s ptr=%u data= %u \n",data,ptr,data);
    >> printf("val=%u data=%s\n",((xyz)data).val,(xyz)data.data);
    >> printf("val=%u data=%s\n",(xyz*)ptr->val,(xyz*)ptr->data);
    >>
    >> }
    >> for the above progrmme i am getting errors like this (compiled in gcc)

    >
    >You should get even more diagnostics from gcc. You have your diagnostic
    >level set far too low. You have no reason at all to think you can cast
    >a char array to a struct. If you *must* play with type punning, almost
    >always a bad idea, here is a better form of your code:
    >
    >#include <stdio.h>
    >#include <string.h>
    >
    >typedef struct
    >{
    > int val;
    > char data[10];
    >} xyz;
    >
    >int main(void)
    >{
    > xyz a = { 10, "abcdefg" };
    > char data[sizeof a];


    But there is no guarantee that data is properly aligned to hold an
    object of type xyz.

    > void *ptr = data;
    > ptr = memcpy(data, &a, sizeof a);
    >
    > printf("%s ptr=%p data= %p\n", data, (void *) ptr, (void *) data);
    > printf("val=%u data=%s\n", (*(xyz *) data).val,
    > (*(xyz *) data).data);


    If data is not properly aligned, this invokes undefined behavior.
    Furthermore, val is a signed int but %u expects an unsigned int.

    > printf("val=%u data=%s\n", ((xyz *) ptr)->val,
    > ((xyz *) ptr)->data);
    > return 0;
    >}



    Remove del for email
     
    Barry Schwarz, Mar 4, 2007
    #3
    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. Replies:
    11
    Views:
    11,375
  2. AddisLig
    Replies:
    0
    Views:
    329
    AddisLig
    Nov 16, 2008
  3. kmw
    Replies:
    5
    Views:
    1,904
    James Kanze
    Oct 29, 2009
  4. Clint Olsen
    Replies:
    6
    Views:
    401
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  5. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    191
    Arndt Jonasson
    Jan 27, 2005
Loading...

Share This Page