copy char arrays

C

chacha

char cSubject[256];
chat *h_ptr[8];

If I want to copy the char array pointed by h_ptr[6] to cSubject
character by character, does the code looke like the following:

strcpy(cSubject+i,*(h_ptr[6])+i);
 
A

Al Bowers

chacha said:
char cSubject[256];
chat *h_ptr[8];

If I want to copy the char array pointed by h_ptr[6] to cSubject
character by character, does the code looke like the following:

strcpy(cSubject+i,*(h_ptr[6])+i);

No, it is not valid.
*(h_ptr[6]) would yield a value representing the first
char in the char array to which h_ptr[6] is pointing.
Function strcpy requires the argument represent a value
pointing to a string.

Since your description is in terms of char arrays (not strings),
you might be interested in using function memcpy. Be careful
not to extend beyond array boundries.

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

int main(void)
{
char cSubject[64];
char *h_ptr[8] = {"HelloMa ","GoodyMa ","RightMa ",
"NightMa ","SeeYaMa ","LoveuMa ",
"AintuMA ", "CantuMA "};
int i;

for(i = 0; i < 8; i++)
memcpy(cSubject+(i*8),h_ptr,8);
puts("Contents of cSubject...");
for(i = 0; i < 64;i++) putchar(cSubject);
putchar('\n');
return 0;
}
 
R

Roy Jan

chacha said:
char cSubject[256];
chat *h_ptr[8];

If I want to copy the char array pointed by h_ptr[6] to cSubject
character by character, does the code looke like the following:

strcpy(cSubject+i,*(h_ptr[6])+i);

*(h_ptr[6]) pointed to a string that cannot add i.

#include <stdio.h>

int main(void)
{
int i = 0;
char cSubject[56],*curoff; // char *cSubject;
char *h_ptr[8] = {
"isalnum(c)",
"isalpha(c)",
"isdigit(c)",
"isupper(c)",
"islower(c)",
"iscntrl(c)",
"isprint(c)",
"isspace(c)",
};

curoff = cSubject;
for (i = 6; i < 8; i++) {
strcpy(curoff,h_ptr);
curoff += strlen(h_ptr);
}
puts(cSubject);
return 0;
}
 
C

chacha

Al said:
No, it is not valid.
*(h_ptr[6]) would yield a value representing the first
char in the char array to which h_ptr[6] is pointing.
Function strcpy requires the argument represent a value
pointing to a string.

Since your description is in terms of char arrays (not strings),
you might be interested in using function memcpy. Be careful
not to extend beyond array boundries.

After a closer look at the codes, I found that "char *h_ptr[8]"
seems to be an array of string pointers as when I print them out
it prints from different places of a long string.

Now if I want to copy the first line starting from where h_ptr[6]
points to, how do I do it?
 
J

Jason

chacha said:
After a closer look at the codes, I found that "char *h_ptr[8]"
seems to be an array of string pointers as when I print them out
it prints from different places of a long string.

Now if I want to copy the first line starting from where h_ptr[6]
points to, how do I do it?

Try to divide and conquor, so to speak. Thinking in pointers to
pointers can make the braein hurt. Try this:

char *h_ptr[8];
char *ptr;
char some_array[256];

/* do some stuff here ... */

ptr = h_ptr[6];

Now simply use ptr as you would any "normal" char *. IE,

strcpy(some_array,ptr);

Later on you can reset ptr to another value, and do the same.

-Jason
 
C

chacha

Jason said:
char *h_ptr[8];
char *ptr;
char some_array[256];

/* do some stuff here ... */

ptr = h_ptr[6];

Now simply use ptr as you would any "normal" char *. IE,

strcpy(some_array,ptr);

Doesn't this copy the *entire* string, not just the first line
which is what I want? I only want to copy up to the first '\n' in
h_ptr[6].
 
A

Al Bowers

chacha said:
Al said:
No, it is not valid.
*(h_ptr[6]) would yield a value representing the first
char in the char array to which h_ptr[6] is pointing.
Function strcpy requires the argument represent a value
pointing to a string.

Since your description is in terms of char arrays (not strings),
you might be interested in using function memcpy. Be careful
not to extend beyond array boundries.


After a closer look at the codes, I found that "char *h_ptr[8]"
seems to be an array of string pointers as when I print them out
it prints from different places of a long string.

Now if I want to copy the first line starting from where h_ptr[6]
points to, how do I do it?

From your comments it seems impossible to decipher and
give you help. You need to be more descriptive with your
question. Show what is in the "long string". Show where
h_ptr pointers are pointing (how they were assigned).
Tell what you mean by "first line". Show
some code that might describe what you are doing.
 
J

Jason

chacha said:
Jason said:
char *h_ptr[8];
char *ptr;
char some_array[256];

/* do some stuff here ... */

ptr = h_ptr[6];

Now simply use ptr as you would any "normal" char *. IE,

strcpy(some_array,ptr);

Doesn't this copy the *entire* string, not just the first line
which is what I want? I only want to copy up to the first '\n' in
h_ptr[6].

Ahhhh! I see! Why didn't you just say that in the first place? Check
out strchr & strtok in <string.h>

-Jason
 

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

Staff online

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,432
Latest member
GTRNorbert

Latest Threads

Top