473,378 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

conversion to non-scalar type requested error

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
2 22472
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tumurbaatar S. | last post by:
Hi, I'm running MySQL 3.23.55 on WinXP and have some problem using non latin charset. I've added these 2 lines under group of my.ini: character-sets-dir = c:/mysql/share/charsets/...
31
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
9
by: Erik Leunissen | last post by:
L.S. I've observed unexpected behaviour regarding the usage of the '#' flag in the conversion specification in the printf() family of functions. Did I detect a bug, or is there something wrong...
14
by: junky_fellow | last post by:
Can anybody please explain this: When a value with integer type is converted to another integer type other than _Bool, if the new type is unsigned, the value is converted by repeatedly...
3
by: gilad | last post by:
This is to announce the beta 2 release of Aumplib. After garnering comments from this newsgroup, I have modified the code to use a more standard style convention. Aumplib is a C# namespace which...
4
by: Påhl Melin | last post by:
I have some problems using conversion operators in C++/CLI. In my project I have two ref class:es Signal and SignalMask and I have an conversion function in Signal to convert Signal:s to...
28
by: James Brown | last post by:
All, I have a series of characters which I need to convert to integer values. Each character is read in turn from a function 'nextch', and hex-digits are identified by the isxdigit function - so...
2
by: John | last post by:
Hi I have a vs2003 database app that use non-bound infragistics controls and all the plumbing is done in code. This works fine in vs2005. After importing to vs2005 via the built-in...
2
by: =?Utf-8?B?U2FtZWVrc2hh?= | last post by:
Suppose there are 2 classes A and B with a int parameter called 'val' in each. Both of them provide a public constructor with int type parameter. Suppose class A provides a explicit conversion...
19
by: fungus | last post by:
I define this class: class foo { std::vector<int>data; public: int operator(int n) { return data; }
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.