Normal program temination

#include<stdio.h>
#include<string.h>
float arr[8] ={0.3,1.7,3.6,3.89,1.9,3.5,6.1,0.78};
int t=4;
void merge (int x ,float z);
char *str;
int main()
{
int a, b, c;
for(a=0;a<5;a++)
{
b=a*8;
for(c=0;c<t;c++)
merge(c,arr[c+b]);
}
printf("%s",str);
return 0;
}

void merge(int x,float z)
{
char *id=NULL,*ptr=NULL;
sprintf(id,"%d",x);
strcat(str,id);
sprintf(ptr,"%f",z);
strcat(str,ptr);

}


The program gets compiled. but the printf() statement does not show the response.
Your program invokes undefined behavior as it tries to dereference the null pointer.

You need to give sprintf a buffer that is large enough to contain the formatted output string; it will not create one for you. A similar issue arises with strcat; the destination buffer needs to be large enough to contain both strings.
Last edited on
Topic archived. No new replies allowed.