By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

conversion to non-scalar type requested error

P: n/a
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

Feb 19 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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;
}

Feb 19 '07 #2

P: n/a
On Mon, 19 Feb 2007 05:41:09 -0500, Martin Ambuhl
<ma*****@earthlink.netwrote:
>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
Mar 4 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.