Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sprintf question

Status
Not open for further replies.

rtnMichael

Programmer
Aug 5, 2002
152
US
ok, why does this give me a bus error?
int mail_num = 0;
char mail_text_array[9999];
...
...
...

mail_function(f,p1,p2,p3,p4,p5,p6,p7,p8,p9)
char *f;
{
sprintf(mail_text_array[mail_num],f,p1,p2,p3,p4,p5,p6,p7,p8,p9);

mail_num++;
}


when this does it just fine:

char mail_text[10];

...
...
...

mail_function(f,p1,p2,p3,p4,p5,p6,p7,p8,p9)
char *f;
{
sprintf(mail_text,f,p1,p2,p3,p4,p5,p6,p7,p8,p9);
}


Can somebody explain?

Thanks
M
 
Hi

1) sprintf requires a char * as the first argument

When you are doing mail_text_array[mail_num] you are pointing to a value in the array which is wrong because it is a char not a char *.
&mail_text_array[mail_num] would have worked. But then mail_num should be 0 otherwise it'll start writing from some random location in the string.

2) Also when it says 2nd argument as const char * what it
expects is the format string that is "%s%s%d" etc... you should not just pass any char pointer. It could lead to runtime errors.

so change your sprintf to

sprintf(mail_text, "%s%s%s%s%s%s%s%s%s%s", p1, p2, p3, p4, p5, p6, p7, p8, p9); I assume all p's are char arrays.

3) Also you need to make sure that mail_text has enough space to accomodate the entire string. Otherwise it could lead to runtime errors.



 
Thanks for the suggestions nkm and dchoulette, but the *f is a char string that is somewhat like a printf statement in itself. Maybe I don't know what I'm talking about, but let me show you what I mean:

// call to function //
mail_num = 0;
mail_function(1,FLAGIT,"%s exists but not %s or %s", var1_path, var2_path, var3_path);

// function //
static int mail_function(level, flagit, f, p1,p2,p3,p4,p5,p6,p7,p8,p9)
char *f;
{
if(printf("%c%s - ",flagit? '+':' ',fmt_time()) == -1)
goto err;

if(printf(f,p1,p2,p3,p4,p5,p6,p7,p8,p9) == -1 || putchar('\n') == EOF || fflush(stdout) == EOF)
{
err: (void) fprintf(stderr,"%s: error %d - %s.\n",
process_name,errno, strerror(errno));

(void) fprintf(stderr,"%s: Can't produce log messages.\n", process_name);
(void) fprintf(stderr,"%s: Terminating.", process_name);
exit(2);
}

if(level <= log_level)
{
// MAX_INPUTS = 10 //
if (mail_num >= MAX_INPUTS)
mail_num = 0;

sprintf(mail_text,f,p1,p2,p3,p4,p5,p6,p7,p8,p9);

mail_text_array[mail_num]=(char *)malloc(strlen(mail_text)+1);
strcpy(mail_text_array[mail_num], mail_text);
mail_num++;
}
return(0);
}

I figured out another way to do it, both with the sprintf first and a strcpy to an array afterwards. I know this may not be the most clean way to do it, but hey, I'm dealing with a deadline here so whatever works in the program works for the company. ;-)

Anyway, thanks for the help!
Mike
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top