LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-04-2004, 11:26 AM   #1
cynthia
LQ Newbie
 
Registered: Sep 2004
Posts: 20

Rep: Reputation: 0
casting


Hi,

I wonder how to cast a char* to a struct.

I know the folowing way:

Code:

char *buffer;
struct X *x;

x = (X *)buffer;



but let's assume the following definition:

Code:

struct X
{
char a[100];
int n;



so, a will 'read' all data until '\0' and then stop and no data will be read to variable n.

how can I fix it?
 
Old 10-04-2004, 11:35 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
memcpy(x, buffer, strlen(buffer));

Or if you want the '\0' also you just use strlen(buffer)+1 instead.
 
Old 10-04-2004, 11:41 AM   #3
cynthia
LQ Newbie
 
Registered: Sep 2004
Posts: 20

Original Poster
Rep: Reputation: 0
thanks.


I tried your advise and it worked partially.

any idea what the problem is with the following code:

Code:
int main()
{
	DemoStruct demo1;
	char a[6];
	a[0] = '1';
	a[1] = '2';
	a[2] = '3';
	a[3] = '\0';
	a[4] = '5';

	char* a1 = a;

	memcpy((void*)&demo1, a1, sizeof(a1) + 1);

	printf("%s\n %d\n", demo1.arr, demo1.i);

	printf("\n");

	memcpy((void*)&demo1, a1, sizeof(a1));
	printf("%s\n %d\n", demo1.arr, demo1.i);
}
the output was:

123
[space]-345235235


notes:
1) notice that at the beginning of the second line there was a space - probably the '\0'.

what is the problem?
why wasn't the int i copied properly?

thanks again

Last edited by cynthia; 10-04-2004 at 12:00 PM.
 
Old 10-04-2004, 12:07 PM   #4
cynthia
LQ Newbie
 
Registered: Sep 2004
Posts: 20

Original Poster
Rep: Reputation: 0
I've made a few more trials, and found the problem is when I try to insert the data from a char* into the int field in Struct X.

how to overcome this problem?
 
Old 10-04-2004, 12:26 PM   #5
cynthia
LQ Newbie
 
Registered: Sep 2004
Posts: 20

Original Poster
Rep: Reputation: 0
Code:
typedef struct demoStructInt
{
	int i;
	int j;
	char c;
}DemoStructInt;



int main()
{
	DemoStructInt demoInt1, demoInt2;


/* TRY A*/

	char* x = "000020003";
	memcpy((void*)&demoInt1, x, sizeof(DemoStructInt));
	printf("%d \n", demoInt1.i);


/ TRY B*/	
	char* y = "1";
	memcpy((void*)&demoInt2, y, sizeof(DemoStructInt));
	printf("%d \n", demoInt1.i);
}

in TRY A the variable demoInt1.i contains garbage but in TRY B it contains 49 (OK - ascii code of '1').

though, I thought that by setting 32 bytes 0000 in char*x, I'll be able to set demoInt1.i to 0 but I was wrong...

how to do it corectly?
 
Old 10-05-2004, 03:42 AM   #6
cynthia
LQ Newbie
 
Registered: Sep 2004
Posts: 20

Original Poster
Rep: Reputation: 0
THE SOLUTION:

typedef struct demoStructInt
{
char num1[4];
char c;
char c1;
char num2[6];
char num3[4];
int x;
int y;
}DemoStructInt;



int main()
{
DemoStructInt demoInt1, demoInt2;

strcpy(demoInt1.num1, "aaa");
strcpy(demoInt1.num2, "ddddd");
strcpy(demoInt1.num3, "eee");
demoInt1.c = 'b';
demoInt1.c1 = 'c';
demoInt1.x = 1;
demoInt1.y = 10;
int size = sizeof(DemoStructInt);

char x[100];

memcpy((char*)x, (char*)&demoInt1, size);

memcpy((char*)&demoInt2, (char*)x, size);
printf("demoInt2.num1 = %s\ndemoInt2.c = %c\ndemoInt2.c1 = %c\ndemoInt2.num2 = %s\ndemoInt2.num3 = %s\ndemoInt2.x = %d\ndemoInt2.y = %d\n\n", demoInt2.num1, demoInt2.c, demoInt2.c1, demoInt2.num2, demoInt2.num3, demoInt2.x, demoInt2.y);


//cout << "size = " << size << endl;
//printf("demoInt1.x (c) = %c, demoInt1.x (d) = %d\n\n", demoInt1.x, demoInt1.x);


//memcpy((char*)&demoInt2, (char*)x, size);
//printf("demoInt2.num1 = %s\ndemoInt2.c = %c\ndemoInt2.c1 = %c\ndemoInt2.num2 = %s\ndemoInt2.num3 = %s\ndemoInt2.x = %d\ndemoInt2.y = %d\n\n", demoInt2.num1, demoInt2.c, demoInt2.c1, demoInt2.num2, demoInt2.num3, demoInt2.x, demoInt2.y);
//printf("demoInt2.x (c) = %c, demoInt2.x (d) = %d\n", demoInt2.x, demoInt2.x);

return 0;
}
 
Old 10-05-2004, 03:58 AM   #7
aiza
LQ Newbie
 
Registered: Jul 2004
Posts: 18

Rep: Reputation: 0
Quote:
Originally posted by cynthia
thanks.


I tried your advise and it worked partially.

any idea what the problem is with the following code:

Code:
int main()
{
	DemoStruct demo1;
	char a[6];
	a[0] = '1';
	a[1] = '2';
	a[2] = '3';
	a[3] = '\0';
	a[4] = '5';

	char* a1 = a;

	memcpy((void*)&demo1, a1, sizeof(a1) + 1);

	printf("%s\n %d\n", demo1.arr, demo1.i);

	printf("\n");

	memcpy((void*)&demo1, a1, sizeof(a1));
	printf("%s\n %d\n", demo1.arr, demo1.i);
}
the output was:

123
[space]-345235235


notes:
1) notice that at the beginning of the second line there was a space - probably the '\0'.

what is the problem?
why wasn't the int i copied properly?

thanks again
- I think sizeof(a1) is not equal sizeof(a) because a1 is just a pointer (the correct is sizeof(DemoStruct))
- char a[6] is not big enough to hold string and integer (sizeof(int)==4?)

Aiza

Last edited by aiza; 10-05-2004 at 03:59 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
A Couple of C++ questions, casting -0- Programming 7 06-27-2005 12:14 PM
casting question in kernel source code linetnew Programming 3 04-18-2005 04:55 AM
How to do type casting in Sparc Assembly Language foxele Programming 1 10-09-2004 04:40 PM
c++, casting, templates kev82 Programming 0 08-24-2004 05:09 PM
C++ Casting bretthoward Programming 1 05-11-2004 01:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration