473,769 Members | 6,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copy char arrays

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);
Nov 14 '05 #1
8 8021


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

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

Nov 14 '05 #2

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

Nov 14 '05 #3
Al Bowers wrote:


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?
Nov 14 '05 #4

chacha wrote:
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_arr ay,ptr);

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

-Jason

Nov 14 '05 #5

Jason wrote:
Try to divide and conquor,
I meant "conquer"

so to speak. Thinking in pointers to pointers can make the braein
and "brain"
hurt.


Looks like my brain is already hurting.

Nov 14 '05 #6
Jason wrote:

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_arr ay,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].
Later on you can reset ptr to another value, and do the same.

-Jason

Nov 14 '05 #7


chacha wrote:
Al Bowers wrote:


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.

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

Nov 14 '05 #8

chacha wrote:
Jason wrote:

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_arr ay,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

Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
18007
by: Matt Garman | last post by:
What is the "best" way to copy a vector of strings to an array of character strings? By "best", I mean most elegantly/tersely written, but without any sacrifice in performance. I'm writing an application using C++ and the STL for handling my data. Unfortunately, I must interact with a (vanilla) C API. I use vectors of strings (for simplicity and less memory hassle), but the function calls for this API require arrays of character...
4
1437
by: dmoos AT esigma-systems DOT de | last post by:
Hi all, i've got a simple question: how does the default copy-constructor act on member-arrays ? Since array-variables are often treated as pointers to the array-element-type, one could think that the default copy- constructor would just copy the address of the first element of the member-array-variables. A simple test-programm reveals, that g++-2.96 instead copies the member-array array-element by array-element.
8
3317
by: RonHiler | last post by:
My copy constructor is crashing my program, and I can't figure out why. I'll try to make the code listing as short as I can. Here are the two headers: class BreakthroughClass { public: BreakthroughClass(); virtual ~BreakthroughClass(); BreakthroughClass(const BreakthroughClass &source);
4
8823
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where a * occurs in the string). This split function should allocate a 2D array of chars and put the split results in different rows. The listing below shows how I started to work on this. To keep the program simple and help focus the program the...
4
1968
by: Mat | last post by:
Hi all. This is my situation: - I have 3 arrays, for example: unsigned char myArray1 = { 11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34, 41, 42, 43, 44
6
3216
by: Soumyadip Rakshit | last post by:
I have a 2D Array of Integers A. I would like to copy it to another array B taking each row at a time. They are both initialized as pointers to pointers. I would like to use something like the following but it doesn't seem to work for (i=0; i < 3; i++) { while(*A++ = *B++) ; }
15
3826
by: Frederick Gotham | last post by:
What's the canonical way to copy an array in C++? If we're copying a POD, we can use memcpy (but there could be a more efficient alternative if we know that the blocks are suitably aligned). Regardless of whether the array consists of POD's, we could use a loop such as: #include <cassert>
20
5095
by: Gary Wessle | last post by:
string a = "abcdef"; aa = a.c_str(); bb = how can I get def from aa in here? thanks
8
2170
by: anon.asdf | last post by:
Hi! OK, lets try "array-copy": { char arrayA; arrayA = (char){1, 2, 3}; } it does *not* work since we're trying to make a fixed array-pointer arrayA, point to another location/address (where there is an
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.