Hello this is my first thread on the forum i tried to search everywhere for a solution but no result...:'(
Im trying to figure out some easy pointer use but still get some stupid error... My fault.
error C2440: '=' : cannot convert from 'char (*)[100]' to 'char'

void split(char *src, char c)
{
	char temp[100];
	int i;
	for(i=0;*src!=0;i++)
	{
		if(i%2==0)
		{
			temp[i]=*src;
			*src++;
		}
		else
			temp[i]=c;
	}
	*src=&temp; //c2440

}
void main()
{
	char *src="eretznehederet", sep='-';
split(src,sep);
printf("%s\n", src);
}

thanks anyway for all the help I can get and no problem for anyone that I helped with this thread.
Seaman34.

Recommended Answers

All 11 Replies

>>*src++;

line 9: remove the star -- you need to increment the pointer, not what the pointer is pointing to src++; line 15: >>*src=&temp; //c2440

You can't do that either. *src is a single character and you can not assign it a pointer to a character array temp. I don't know what you are trying to do with that line.

That entire loop is wrong. It is assigning every other character in src to every other byte in temp array. For odd numbers of i the byte in temp will just contain some junk random stuff which was on the stack then temp was allocated on the stack. I doubt that was your intent.

if i add printf("%s",temp) line 16 the output is what I want
e-r-e-t-z-n-e-h-e-d-e-r-e-t
i just need to copy temp back to src so line 9 work as it should.
line 20,21,22 is what i got from the work.
May it work if i use strcopy?

If you are writing C++ you should be using cout , not printf()

I'm not Im in a C course old school...

if i add printf("%s",temp) line 16 the output is what I want
e-r-e-t-z-n-e-h-e-d-e-r-e-t
i just need to copy temp back to src so line 9 work as it should.
line 20,21,22 is what i got from the work.
May it work if i use strcopy?

If you want the source string to retain the modification it can not be a read-only string.
char *src makes the string read-only. Any attempt to modify it will produce segmentation fault error.

char *src makes the string read-only. Any attempt to modify it will produce segmentation fault error.

I think you mean const char *src is read-only and the compiler won't let you write to it.

I think you mean const char *src is read-only and the compiler won't let you write to it.

No, I know what I mean, and I mean what I know.

char *src="eretznehederet"

And it doesn't have anything to do with the function, but rather with the idea of the OP wanting to copy the modified temp string into it.

No, I know what I mean, and I mean what I know.

char *src="eretznehederet"

And it doesn't have anything to do with the function, but rather with the idea of the OP wanting to copy the modified temp string into it.

Oh I see what you mean now. It wasn't clear in your previous post what you were talking about. I thought it was the function parameter.

so there is no way to copy it? or may i flush it then rewrite it or clear the memory then opening a function making a static one after

You need to declare it like this: char src[100]="eretznehederet"; so that variable src is not a pointer into read-only memory. you can change the 100 to anything you want, as long as its large enough to hold all the text you want to put into it.

Next, at the end of that split function use strcpy() to copy the contents of temp back into src. strcpy(src,temp);

The problem is that you are trying to create a reference to a local variable and send it outside. It is not a good idea, 'cause the returned value may be corrupted in any moment. The best solution is to allocate data dynamically by calloc(). Another solution is to make the variable temp static (however, in this case each new call of this function can change the results of previous calls. You may use static char temp[100]; only for printing the result after each call or for copying it by strcopy, but not for storeing references to it).

Also, the first argument must be a pointer to pointer to char:
1. void split(char **src, char c)
.......
21. split(&src,sep);

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.