-
strcpy and strdup?
*buf[0]={"Tension"};
How do I use 'strcpy' or 'strdup' in this situation below:
buf[0][0]='T' copy into buf1[0][0];
buf[0][1]='e' copy into buf1[0][1];
buf[0][2]='n' copy into buf1[0][2];
buf[0][3]='s' copy into buf1[1][0];
buf[0][4]='i' copy into buf1[1][1];
buf[0][5]='o' copy into buf1[1][2];
buf[0][6]='n' copy into buf1[2][0];
In my code I tried like this in loops but it generates some warning:
strcpy(buf1[a][b], buf[c][b]);
warning C4024: 'strcpy' : different types for formal and
actual parameter 1.
-
I think you only need to use the first bracket in the strcpy as the second refers to a individual character in the string and strcpy copies strings not characters
strcpy(buf1[a], buf[c]);
I havent tried this as i use pointers when dealing with strings(much easier) so I could be wrong, but give it a go
-
same problem.
The same problem exists even I write like this:
buf1[a][b] = buf[c][b]
-
source code
#include <stdio.h>
#include <string.h>
int main()
{
unsigned int a, b, c, pc, d;
char pbuf[10][5];
char *array[3]={ "smelly thingy",
"Hello world man",
"piece of cake hellol asdjfksd asdfsf adsfa sfaafa"};
for(a=0; a<50; a+=5)
{
for(b=0, pc=1; b<3, pc<50; b++, pc++)
{
for(c=a, d=0; c<(a+5), d<5; c++, d++)
{
if ( c < strlen(array[b]) )
{
pbuf[pc][d] = array[b][c];
printf("%c", pbuf[pc][d]);
}
}
printf( "\n" );
}
printf( "-----\n" );
}
return 0;
}
The buf and buf1 are just an example but in this code they refer to 'pbuf' and 'array'.
The reason why I put 5 bytes per 5 bytes into independent array is to make protocol. After splitting into 5 bytes and storing into independent array i.e. pbuf[pc][d], I will use strcat or strncat to concatenate with a header just like in protocolling but the compiler generates some error.
The way that I declare the pbuf[10][5] again is not memory friendly.
Its there any better way to do this. Suggestions are welcome.
Lastly, thank you for your consideration, Salem. Same goes to C_Coder, thank you for your explanation.:)
-
Ug. That just looks nasty. Here's a thought:
Code:
for(b=0, pc=1; b<3, pc<50; b++, pc++)
{
for(c=a, d=0; c<(a+5), d<5; c++, d++)
{
if ( c < strlen(array[b]) )
{
pbuf[pc][d] = array[b][c];
printf("%c", pbuf[pc][d]);
Can anyone here spot the obvious flaw with this logic?
Ah, here it is:
pbuf[ up to 50! ][ upto 5 ]
Ok, so where have you declared a pbuf array that is potentially 50x5? You haven't.
Quzah.
-
typo error!
The variable pc = a not pc=1! sorry!
-
Dear Quzah,
The problem still exists, eventhough I declared pbuf[50][5].
-
Brilliant!
Dear Salem,
Thank you for your brilliant source code but this code unable for me to do the time division multiplexing if compared to my previous code. Anyway, I will try to do the time division multiplexing by expanding your code. Thanks again!