Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > conversion to non-scalar type requested error

Reply
Thread Tools

conversion to non-scalar type requested error

 
 
venu reddy
Guest
Posts: n/a
 
      02-19-2007
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

 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      02-19-2007
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;
}

 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      03-04-2007
On Mon, 19 Feb 2007 05:41:09 -0500, Martin Ambuhl
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conversion from "" to non-sclar trpe " " requested. AddisLig C++ 0 11-16-2008 12:25 PM
conversion from '...' to non-scalar type '...' requested tthunder@gmx.de C++ 11 06-05-2006 09:02 PM
Conversion from type 'DBNull' to type 'String' is not valid Chris ASP .Net 2 05-11-2006 08:20 AM
conversion from type 'DBNull' to type 'float' is not valid ibiza ASP .Net 2 01-27-2006 09:57 PM
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. luna ASP .Net 1 02-13-2004 01:15 PM



Advertisments