update : for some reason, the example I tried that *didn't* work must have
been different from what I sent to the list, because that works fine..

what I'm really trying to do is a little more involved.. I'm screwing
around with GTK+ and I wanted to call a messagebox function that I wrote
with a message saying what button I clicked on.. the callback function
takes a paramater of type gpointer, I cast the string "button 1" to
gpointer in the function call, then cast it back to (char *) in the
sprintf() call

i.e.:

sprintf(string, "you pressed %s", (char *) data);

segfaults
buuuuut

printf("you pressed %s", (char *) data);

works fine.. the GTK+ tutorial uses the same example, but they use g_print
instead of printf.. the point is that both of those work, but sprintf
won't.. sprintf *DID* work inthe example i sent to the list, don't ask me
why it didn't the first time ;p

anyway, I'm just screwing around, learning gtk+ and I wanted to solve this
problem.. I'm more familiar wtih c++ than with C but I'm converting so
bear with me :)

On Tue, 25 May 1999, Michael Yuan wrote:

> 
> Might sound obvious ... But have you allocated memory for your string
> pointer "string2"?
> 
> Michael
> 
> On Tue, 25 May 1999, Justin Ryan [PHT] wrote:
> 
> > hey guys, any idea why my program would segfault whenever I try to use
> > sprintf? example program I wrote to try and find my error:
> > 
> > #include <stdio.h>
> > 
> > int main()
> > {
> > char *string;
> > char *string2;
> > 
> > string = "blah";
> > 
> > sprintf(string2, "%s is the string", string);
> > 
> > printf("the string says %s", string2);
> > 
> > return (0);
> > }
> > 
> > this segfaults, if i just use printf with string it works fine.. any
> > input?
> > 
> > -Justin
> > 
> > |--------------------------------------------------
> > |Justin Ryan
> > |Developer Relations / Support Associate
> > |Pacific HiTech / TurboLinux
> > |http://www.turbolinux.com / [EMAIL PROTECTED]
> > |WebMaster, PCHelp - http://computers.iwz.com
> > |[EMAIL PROTECTED]
> > |--------------------------------------------------
> > 
> > ---------------------------------------------------------------------------
> > Send administrative requests to [EMAIL PROTECTED]
> > 
> 

|--------------------------------------------------
|Justin Ryan
|Developer Relations / Support Associate
|Pacific HiTech / TurboLinux
|http://www.turbolinux.com / [EMAIL PROTECTED]
|WebMaster, PCHelp - http://computers.iwz.com
|[EMAIL PROTECTED]
|--------------------------------------------------

---------------------------------------------------------------------------
Send administrative requests to [EMAIL PROTECTED]

Reply via email to