Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old September 5th, 2008, 07:05 AM
Ashit Vora
Guest
 
Posts: n/a
Default Problem with Char*

I friends,
I 'm facing a weird prob.

the code is

int total_buf_size=100;
char *buf;
int type= 0xFE10;

buf =(char*) malloc (total_buf_size);

memcpy(buf,&type, 10);
printf("Buf:: %s\n",buf);

The printf doesnt print anything but just "Buf::"

can anyone pls help me out resolve this???
Thanks a ton
  #2  
Old September 5th, 2008, 07:25 AM
Richard Heathfield
Guest
 
Posts: n/a
Default Re: Problem with Char*

Ashit Vora said:
Quote:
the code is
>
int total_buf_size=100;
char *buf;
int type= 0xFE10;
>
buf =(char*) malloc (total_buf_size);
>
memcpy(buf,&type, 10);
printf("Buf:: %s\n",buf);
No, it isn't. The above will not even compile. Presumably you have some
real code that you can compile. Why not show that to us? If you can't find
the problem yourself, full disclosure is your best strategy.
Quote:
The printf doesnt print anything but just "Buf::"
What did you expect it to print, and why?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
  #3  
Old September 5th, 2008, 07:35 AM
eldredge.n@gmail.com
Guest
 
Posts: n/a
Default Re: Problem with Char*

On Sep 4, 10:59 pm, Ashit Vora <a.k.v...@gmail.comwrote:
Quote:
I friends,
I 'm facing a weird prob.
>
the code is
>
int total_buf_size=100;
char *buf;
int type= 0xFE10;
>
buf =(char*) malloc (total_buf_size);
>
memcpy(buf,&type, 10);
printf("Buf:: %s\n",buf);
>
The printf doesnt print anything but just "Buf::"
>
can anyone pls help me out resolve this???
Thanks a ton
ObCLC: You're invoking undefined behavior, anything could happen, etc.

There are a number of problems.

First, why are you copying 10 bytes? Your integer probably isn't that
large. You probably want to use sizeof(int) here instead.

Second, what kind of system are you on? If it is big-endian with 32-
bit integers, the first byte of type (which will be the first byte in
buf) would be the most-significant byte, which is 0, and acts as the
terminating null of the string.

Just what is it that you're trying to accomplish, and what do you
expect it to do? If you want to print out the character with value
0xFE, just do

printf("character looks like %c\n", 0xfe);

or even

puts("character is \xfe");
  #4  
Old September 5th, 2008, 07:45 AM
Martin Ambuhl
Guest
 
Posts: n/a
Default Re: Problem with Char*

Ashit Vora wrote:
Quote:
I friends,
I 'm facing a weird prob.
>
the code is
No, it isn't. If anyone cares to look at the uncompilable original
lines can look at the original post.
Try compiling this and running it on your machine:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int main(void)
{
int total_buf_size = 100;
char *buf;
int type = 0xFE10;

buf = malloc(total_buf_size); /* removed silly cast */
if (!buf) { /* and added error check */
fprintf(stderr, "attempt to allocated %d bytes for\n"
"buf failed. Giving up ...\n", total_buf_size);
exit(EXIT_FAILURE);
}

/* key addition */
if (sizeof type < 10) {
fprintf(stderr,
"I'm not even going to bother with an insane attempt\n"
"to move 10 bytes from an object only %zu bytes "
"long.\n",
sizeof type);
free(buf);
exit(EXIT_FAILURE);
}
memcpy(buf, &type, 10);
printf("Buf:: %s\n", buf);
free(buf);
return 0;
}

Quote:
The printf doesnt print anything but just "Buf::"
Uncompilable code will very rarely have output that makes any sense.
Quote:
>
can anyone pls help me out resolve this???
Stop trying to move 10 bytes from something not 10 bytes long.
And learn how to spell "please".

Quote:
Thanks a ton
 

Bookmarks

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.