Click here to Skip to main content
15,885,653 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi Guys,

I've been scouring the net but I can't quite get my head around how to copy a char array and pass the values to another function.

Say I have 3 arrays;

char buf1[50], buf2[50], buf3[50]

I am trying to copy and pass the data within each buffer to another function, but all this talk of pointers has got my head in a spin and even the word "function" has lost all meaning to me!

Would really appreciate the help.
Posted
Updated 27-Mar-11 5:30am
v2
Comments
Dalek Dave 27-Mar-11 11:31am    
Edited for Grammar and Spelling.

To copy one buffer to another, you can do this;

for(int i=0;i<50;i++)
    buf2[i] = buf1[i];


To pass the buffer to a function you can use this;
void myfunction(char *buffer)
{
    // do something with buffer here, f.e. set all elements to 0
    for(int i=0;i<;50;i++)
        buffer[i] = 0;

}

void main(void)
{
    char buf1[50], buf2[50], buf3[50];

    // initialise all elements of each buffer to 0, using myfunction()
    myfunction(buf1);
    myfunction(buf2);
    myfunction(buf3);
}
 
Share this answer
 
Comments
Dalek Dave 27-Mar-11 11:31am    
Good Answer.
[no name] 27-Mar-11 11:33am    
Thanks! Your vote gave me gold reputation ;)
Nemanja Trifunovic 27-Mar-11 17:06pm    
It is not a good idea to hardcode the array size within the function. It is better to pass it as another parameter instead.
[no name] 28-Mar-11 2:51am    
I agree, and there should probably be more meaningful names than myfunction() and buf1/buf2/buf3 in a real aplication.
To answer the question on how to pass an array to another function though, I think this code demonstrates what to do.
Nemanja Trifunovic 28-Mar-11 10:24am    
It is not about style here. Because array decays into pointers when passed as function arguments, there is no way to determine the size of the array from within a function. Therefore, either the size needs to be passed as a separate parameter, or array needs to be terminated by some well-known value that the function will recognize.
Surely there is a good explanation somewhere, but I will write my own.

In C/C++ (and pretty much every other language) an array is just a pointer to the first element in the array.
In C/C++ it also allocates memory on the stack, rather than you allocating memory with new, but that isn't really important to how they work.

So, we have 3 arrays (pointers) buf1, buf2 and buf3.
Each of these is just a pointer to a peice of memory, for this example:
buf1=0x1000
buf2=0x1100
buf3=0x1200

Then the contents of memory starting at 0x1000 is the elements of the array, say for example you have the code:
char buf1[50] = "Hello";
char buf2[50] = "World";
char buf3[50] = "Array";

Then the memory would look like this:
Offset 0x1000:
'H', 'e', 'l', 'l', 'o', '\0', (rest of 50 chars in undefined)
Offset 0x1100:
'W', 'o', 'r', 'l', 'd', '\0', (rest of 50 chars in undefined)
Offset 0x1200:
'A', 'r', 'r', 'a', 'y', '\0', (rest of 50 chars in undefined)

Where each character is 1 byte.

When you pass this pointer into a function, it just passes a copy of the memory location, for example:
void Print(char *str) {
	printf(str); //print variable <str> to the screen. <str> points to a bit of memory that has a string.
}

int main() {
	char buf1[50] = "Hello"; //Memory location 0x1000
	char buf2[50] = "World"; //Memory location 0x1100
	char buf3[50] = "Array"; //Memory location 0x1200
	Print(buf1); //This calls Print(0x1000)
}


Then what happens is the pointer to buf1 is passed to Print(), which is the memory location 0x1000.
This does not create a copy of the memory, and so if the function Print() changes anything in the memory, it will affect the string in both Print() and main().

If nothing is changed in Print(), in this example there isn't, then there is no need to copy the memory.
If you change something and you want this change to go back to the calling function main() (such as convert to upper case) you MUST NOT copy the memory (unless you want a copy in the original case as well).
If something is changed in Print() and you DON'T want this to change the array in main() then you need to copy the array, this can be done with another buffer or some allocated memory:
char buf1[50] = "Hello";
char buf1Copy[50];
strcpy(buf1Copy, buf1); //Copies the string (5 characters + 1 null terminating character '\0'). Only works for strings.
/* OR... */
memcpy(buf1Copy, buf1, sizeof(buf1)); //Copies all 50 bytes of buf1 into buf1Copy. Works for any array.
 
Share this answer
 
Comments
Аslam Iqbal 27-Mar-11 10:02am    
Perfect solution with basic description. my 5
Sandeep Mewara 27-Mar-11 11:04am    
My 5!
Proposed as answer.
Sandeep Mewara 27-Mar-11 11:04am    
Comment from OP:
WOW...I'll get started on that then and let you know how I go..

Thanks andrew
Dalek Dave 27-Mar-11 11:32am    
Excellent, gets a 5.
CPallini 27-Mar-11 17:08pm    
5. [pedantic mode]Anyway I would use memcpy(buf1Copy, buf1, sizeof(buf1Copy));[/pedantic mode].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900