i'm trying copy a string to another and adding space between the names
but it show a warning c4700
and i don't know why???

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *convert(char *string)
{	
	char *p=string;
	char *tmp;
	while (*string==' ')string++;
	while (*string)
	{			
		*tmp++=*string++;
		while(*string >= 'a' && *string <='z')
			*tmp++=*string++;	
		strcat(tmp,"D");
		tmp++;
	}
	return p;
}

void main()
{	
	char name[]="SasonSasoniBenSason";
	char*str=convert(name);
	printf("%s",str);
}

line 12: Since temp is an uninitialized pointer this line will most certainly crash your program. You have to allocate memory using malloc() before starting the loop on line 10. Line 8 should read: char *tmp = malloc(strlen(string)+1); .

line 7: You don't need to save the pointer string, you need to make another copy of the pointer tmp after it has been allocated by malloc().

char *tmp = malloc(strlen(string)+1);
char *p = tmp;
// rest of your code here

line 25: you need to call free() to deallocate the memory that is returned by the convert() function

giving new programmers "malloc" is like giving them rope to hang themselves with or a gun to shoot themselves in the foot with.

even experienced programmers should avoid the use of malloc unless absolutely necessary. which it usually isn't.

declare the local "tmp" string in the subroutine as a char array of some length, and do a check for the strlen( ) of the input argument if you want a check.

better yet, don't have the subroutine even make a copy, just modify the string that is passed into the function in place. if you need to keep an unmodified copy of the original, have the caller (main) make and keep the copy.

giving new programmers "malloc" is like giving them rope to hang themselves with or a gun to shoot themselves in the foot with.

malloc() is only one of may may ways newbes can kill themselves. Just because you don't own a gun doesn't mean you can't slit your wrists or jump over a cliff :)

even experienced programmers should avoid the use of malloc unless absolutely necessary. which it usually isn't.

My guess is that you have not done much professional programming.

declare the local "tmp" string in the subroutine as a char array of some length, and do a check for the strlen( ) of the input argument if you want a check.

That will rarely work correctly. You might get lucky but there is no guarentee that the array won't be big enough. Sure you can use strlen() to check on the length of the string and not do anything if the buffer isn't big enough, but that doesn't solve the problem. The only way to guarentee the new buffer is big enough -- without declaring very huge arrays -- is by using malloc() to allocate anough memory.

better yet, don't have the subroutine even make a copy, just modify the string that is passed into the function in place. if you need to keep an unmodified copy of the original, have the caller (main) make and keep the copy.

We don't know the program requirements. That will work as long as its not a string leteral.

My guess is that you have not done much professional programming

then your guess would be wrong.

then your guess would be wrong.

Wouldn't be the first time :)

sorry for any misunderstanding... im not saying malloc should never be used. its a very powerful tool that has its place.

im saying it shouldnt be used by beginners when there are other options, because they come to rely on its use without understanding the dangers.

and experienced programmers often make too much use of it themselves. its (mis)use is the cause of some of the worst errors from a debugging standpoint.

Pointers and memory leaks in C

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.