Normal program temination

Dec 1, 2016 at 4:31am
#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.
Dec 1, 2016 at 6:29am
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 Dec 1, 2016 at 6:30am
Topic archived. No new replies allowed.