I'm running ubuntu on x86_32...and I keep getting segmentation fault while running this program.

#include<stdio.h>
#include<stddef.h>
char *memcp(char *dest, const char *src, size_t n)
{

char *dp = dest;
const char *sp = src;
while(n--)
*dp++ = *sp++;
return dest;

}

int main()
{

char *s = "abcde";
char *d;
char *r = memcp(d,s,6);
printf("%s",r);

return(0);
}

The problem with this code is that it is running on my friend's x86_64 machine on windows as well as ubuntu. Please help me out ..

Recommended Answers

All 2 Replies

In the main() function you have not allocated memory for the pointer d , In this code you are trying to store values in the unallocated memory which leads to undefined behavior.

commented: Agreed!! +1

>>char *r = memcp(d,s,6);

do you mean to call memcpy()? There is rarely a reason to catch the return value of memcmp() because all it returns is the destination buffer pointer. It has no error checking.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.