Segfault on type cast!

Discuss Programming

Segfault on type cast!

Postby ZiaTioN » Mon Feb 23, 2004 7:50 pm

I am running into a weird situation where I am trying to print an integer value as a string using type casting which has been the way I have done it in the past and I am getting a segfault on the printf statement.

Code: Select all
printf((char*)statbuf.st_size);

That is the syntax of the printf statement. statbuf is just a buffer I use to hold the results of an lstat. Here is how I have it defined:
Code: Select all
struct stat statbuf;


Any ideas? If it would help I could post more code but the rest works fine.
ZiaTioN
administrator
administrator
 
Posts: 460
Joined: Tue Apr 08, 2003 3:28 pm

Postby Void Main » Mon Feb 23, 2004 8:21 pm

I may be missing something but don't you need the format as well?

e.g.
printf("%s",(char*)statbuf.st_size);

P.S. Did you see my last post on your "pwd" thread?
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby ZiaTioN » Mon Feb 23, 2004 9:44 pm

Yeah I tried the formatting too but got the same segfault.

And yes I did see your last posts on the pwd thread. Thanks for putting in the work to find the actual source. I have not had time to give it any attention yet though but I will.
ZiaTioN
administrator
administrator
 
Posts: 460
Joined: Tue Apr 08, 2003 3:28 pm

Postby Void Main » Mon Feb 23, 2004 10:04 pm

Actually, this works for me:

Code: Select all
printf("size=%u\n",statbuf.st_size);
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby ZiaTioN » Mon Feb 23, 2004 10:13 pm

Yeah unsigned int does work, so does %d (digit). I don't know why I did not hink of that. LOL..

I actually use:
Code: Select all
printf("%d\n", (int)statbuf.st_size);


I had to type cast to int because I compile with "-Wall" and it was giving me a warning.
ZiaTioN
administrator
administrator
 
Posts: 460
Joined: Tue Apr 08, 2003 3:28 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron