Copy string of chars

C

Coolm@x

Hi all,
I want to write my own explode (split) function and I need copy string
char by char. I learn pointers and my knowledge about them isn't so big.

int func(char delimiter, char *expression, char **values) {
(...)
//why I can't write this:
memcpy(values[tmplen-1],expression,1);

//only this weird expression work:
memcpy((*(values+i))+(tmplen-1),&expression[n],1);
}

int main() {
char **val;
val = malloc(sizeof(*val));
func(';', "foo;bar", val);
return EXIT_SUCCESS;
}

I know if I declare pointer to char variable:
char *word;
word = malloc(5*(sizeof(*word)));

'[]' - works as '*' operator (If i read good tutorials :) )
word[0] // same as *word
....
word[4] // *(word+4)

so why first way to use of 2d array of chars doesn't work with memcpy,
strcpy (and I think other similar functions)? I would appreciate any help.
--
Best regards,
- Mateusz Pa³osz
[ e-mail: matp dot sa a-t gmail dot com ]
[ JID: (e-mail address removed) ]
[ Pomó¿ ulepszyæ usenet: http://twovoyagers.com/improve-usenet.org/ ]
 
L

Lew Pitcher

Hi all,
I want to write my own explode (split) function and I need copy string
char by char.

Looking ahead to your code, I wonder why you made a simple thing so
complicated. A character-by-character copy is pretty simple to code, as is
a string split function. You've really taken the long way around on this
one.
I learn pointers and my knowledge about them isn't so big.
int func(char delimiter, char *expression, char **values) {
(...)
//why I can't write this
memcpy(values[tmplen-1],expression,1);


1) What is tmplen? How is it computed
2) /values/ is declared as a pointer_to pointer_to char. But, you want to
use it as a two-dimensional array. Well, to find values[i+1], the compiler
will need to know how many values[][] there are. At this point in your
code, you have no way to tell the compiler the number of 2nd-level elements
in your (intended) 2-dimensional array. So, the compiler won't be able to
determine where values[i-1][...] ends and values[...] begins. That's why
this statement fails; the maximum value of the 2nd array qualifier is
unknown to the compiler, and you have no way of making it known.

//only this weird expression work:
memcpy((*(values+i))+(tmplen-1),&expression[n],1);

Again, what is tmplen?

Read the expression (specifically, the (*(values+i))+(tmplen-1) part) and
see what it does...

The first argument of your call to memcpy():
values is a pointer_to pointer_to char
values+1 is a pointer_to pointer_to char, one past values
*(values+1) is a pointer_to char, the 2nd in a (supposed) pointer array
tmplen is (presumably) an integer
tmplen-1 is (presumably) an integer, one less than tmplen
(*(values+1))+(tmplen-1) is a pointer_to char, pointing to the tmplen'th
character of the (supposed character array) pointed to by (values+1)

The 2nd argument of your call to memcpy()
expression is a pointer_to char
expression[n] is identical to *(expression+n), a char in the n+1'th
position of the (presumed) array pointed to by expression
&expression[n] is the address of that character, equivalent to
(expression+n), a pointer_to char

The 3rd argument of your call to memcpy():
is the integer 1

memcpy() is defined as taking three arguments:
argument 1 is a pointer_to char that will denote the destination of the
copy data
argument 2 is a pointer_to char that will denote the source of the copy
data
argument 3 is a size_t indicating the number of characters to copy

So, you got the arguments all set to the correct type. Should the compiler
complain?

}

int main() {
char **val;
val = malloc(sizeof(*val));
func(';', "foo;bar", val);
return EXIT_SUCCESS;
}

I know if I declare pointer to char variable:
char *word;
word = malloc(5*(sizeof(*word)));

'[]' - works as '*' operator (If i read good tutorials :) )
word[0] // same as *word
...
word[4] // *(word+4)

so why first way to use of 2d array of chars doesn't work with memcpy,
strcpy (and I think other similar functions)? I would appreciate any help.

Because, the function func() doesn't know that it /is/ a 2d array, nor does
it know the dimensions of the array. It knows only that you passed a
pointer, and knows nothing of the object that is being pointed to.
 
E

Eric Sosman

Hi all,
I want to write my own explode (split) function and I need copy string
char by char. I learn pointers and my knowledge about them isn't so big.

int func(char delimiter, char *expression, char **values) {
(...)
//why I can't write this:
memcpy(values[tmplen-1],expression,1);


Because the first two arguments are chars, not pointers.
Let's look at the second argument first, because it's simpler.
In expression, expression is a char*, a pointer to a char
object. expression is the value of the i'th char after the
one expression itself points at. You seem to want a pointer to
that i'th character, not the character value itself. To get
such a pointer, you could write &expression or you could
write expression+i (the two are equivalent).

The first argument has a similar difficulty, but with one
more level of indirection. In values[tmplen-1], values is
a char**, a pointer to a char* object, a pointer to a pointer
to a char object. So values is the i'th of those char*
pointers, and values[tmplen-1] is the tmplen-1st char in
that i'th group. Not a pointer to the char, but the value of
the char. You probably want one of

&values[tmplen-1]
or
values+tmplen-1

.... which are, again, equivalent.
//only this weird expression work:
memcpy((*(values+i))+(tmplen-1),&expression[n],1);

That will work (barring the substitution of n for i), but
it's needlessly complicated. Actually, memcpy() itself is
needlessly complicated if all you want to do is copy one
single character:

values[tmplen-1] = expression[n]; /* or ... */
}

int main() {
char **val;
val = malloc(sizeof(*val));
func(';', "foo;bar", val);
return EXIT_SUCCESS;
}

I know if I declare pointer to char variable:
char *word;
word = malloc(5*(sizeof(*word)));

'[]' - works as '*' operator (If i read good tutorials :) )
word[0] // same as *word
...
word[4] // *(word+4)

Yes. Formally, for any pointer p and any integer k,
p[k] means exactly the same thing as *(p+k).
so why first way to use of 2d array of chars doesn't work with memcpy,
strcpy (and I think other similar functions)? I would appreciate any help.

The comp.lang.c Frequently Asked Questions (FAQ) site at
<http://www.c-faq.com/> has a good section about arrays and
pointers. I think you might find it helpful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top