Connecting Tech Pros Worldwide Forums | Help | Site Map

copy char arrays

chacha
Guest
 
Posts: n/a
#1: Nov 14 '05
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);

Al Bowers
Guest
 
Posts: n/a
#2: Nov 14 '05

re: copy char arrays




chacha wrote:[color=blue]
> 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);[/color]

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[i],8);
puts("Contents of cSubject...");
for(i = 0; i < 64;i++) putchar(cSubject[i]);
putchar('\n');
return 0;
}





--
Al Bowers
Tampa, Fl USA
mailto: xabowers@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Roy Jan
Guest
 
Posts: n/a
#3: Nov 14 '05

re: copy char arrays



chacha wrote:[color=blue]
> 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);[/color]

*(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[i]);
curoff += strlen(h_ptr[i]);
}
puts(cSubject);
return 0;
}

chacha
Guest
 
Posts: n/a
#4: Nov 14 '05

re: copy char arrays


Al Bowers wrote:[color=blue]
>
>
> 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.
>[/color]

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?
Jason
Guest
 
Posts: n/a
#5: Nov 14 '05

re: copy char arrays



chacha wrote:[color=blue]
> 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?[/color]

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

Jason
Guest
 
Posts: n/a
#6: Nov 14 '05

re: copy char arrays



Jason wrote:[color=blue]
> Try to divide and conquor,[/color]

I meant "conquer"

so to speak. Thinking in pointers to[color=blue]
> pointers can make the braein[/color]

and "brain"
[color=blue]
> hurt.[/color]

Looks like my brain is already hurting.

chacha
Guest
 
Posts: n/a
#7: Nov 14 '05

re: copy char arrays


Jason wrote:[color=blue]
>
> 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);
>[/color]

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].
[color=blue]
> Later on you can reset ptr to another value, and do the same.
>
> -Jason
>[/color]
Al Bowers
Guest
 
Posts: n/a
#8: Nov 14 '05

re: copy char arrays




chacha wrote:[color=blue]
> Al Bowers wrote:
>[color=green]
>>
>>
>>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.
>>[/color]
>
>
> 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?[/color]

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.

--
Al Bowers
Tampa, Fl USA
mailto: xabowers@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Jason
Guest
 
Posts: n/a
#9: Nov 14 '05

re: copy char arrays



chacha wrote:[color=blue]
> Jason wrote:[color=green]
> >
> > 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);
> >[/color]
>
> 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].[/color]

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

-Jason

Closed Thread