C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 30th, 2006, 04:48 PM
cheb cheb is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2006
Posts: 1 cheb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 39 m 53 sec
Reputation Power: 0
Copy char array

Hi,
I'm trying to copy a char array to another. After that fill the array with char 0. For example if I have unsigned char a[20] and unsigned char b[64], then copy a to b and fill the rest of the array with char 0. This program must only works with array of char. Working with strings would be much easier, but with char array i donīt know how to do this. Thanks

Reply With Quote
  #2  
Old May 30th, 2006, 05:06 PM
kicken's Avatar
kicken kicken is offline
Wiser? Not exactly.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: May 2001
Location: Bonita Springs, FL
Posts: 5,654 kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)kicken User rank is General 37th Grade (Above 100000 Reputation Level)  Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6Folding Points: 2670569 Folding Title: Super Ultimate Folder - Level 6
Time spent in forums: 2 Months 2 Weeks 2 Days 5 h 1 m 44 sec
Reputation Power: 3436
Quote:
Originally Posted by cheb
Hi,
I'm trying to copy a char array to another. After that fill the array with char 0. For example if I have unsigned char a[20] and unsigned char b[64], then copy a to b and fill the rest of the array with char 0. This program must only works with array of char. Working with strings would be much easier, but with char array i donīt know how to do this. Thanks


A standard for loop/assignment operation would wor If youre allowed, you could just use a memcpy/memset statement too. Sounds like a homework problem to me, therefore I'm not going to provide any code.
__________________
Recycle your old CD's, don't just trash them


Spidermonkey Tutorial;

If I helped out out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud

Reply With Quote
  #3  
Old May 30th, 2006, 06:45 PM
para45's Avatar
para45 para45 is offline
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 264 para45 User rank is First Lieutenant (10000 - 20000 Reputation Level)para45 User rank is First Lieutenant (10000 - 20000 Reputation Level)para45 User rank is First Lieutenant (10000 - 20000 Reputation Level)para45 User rank is First Lieutenant (10000 - 20000 Reputation Level)para45 User rank is First Lieutenant (10000 - 20000 Reputation Level)para45 User rank is First Lieutenant (10000 - 20000 Reputation Level)para45 User rank is First Lieutenant (10000 - 20000 Reputation Level)para45 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 6 Days 2 h 28 m 19 sec
Reputation Power: 123
Here's some pseudo-code. src is the array that you are copying from, and dest is the array you are copying to

Code:
for each character in src
   dest = assoicated src character
for the rest of the characters
   assoicated dest = 0


It may look like a restatement of what you asked, but that should be it.

For the second loop you can calculcate the total number of iterations by subtracting the length of src from dest, then add to that the length of src for the assignment offsets into the array.

Reply With Quote
  #4  
Old May 30th, 2006, 06:58 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,385 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 30 m 25 sec
Reputation Power: 4080
Probably easier (but less efficient) to set all elements of b to zero initially and then simply copy a to b.

There is also another way. HINT: One of the standard C functions does this already.

SCORPY's PUZZLE OF THE WEEK: PM me the function name and you'll appear on my sig for a week.
Comments on this post
mateoc15 agrees: Haha, I wish I had seen this just after the posting!
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Reply With Quote
  #5  
Old May 30th, 2006, 08:10 PM
ComputerPhreak's Avatar
ComputerPhreak ComputerPhreak is offline
Super User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2004
Posts: 648 ComputerPhreak User rank is Second Lieutenant (5000 - 10000 Reputation Level)ComputerPhreak User rank is Second Lieutenant (5000 - 10000 Reputation Level)ComputerPhreak User rank is Second Lieutenant (5000 - 10000 Reputation Level)ComputerPhreak User rank is Second Lieutenant (5000 - 10000 Reputation Level)ComputerPhreak User rank is Second Lieutenant (5000 - 10000 Reputation Level)ComputerPhreak User rank is Second Lieutenant (5000 - 10000 Reputation Level)ComputerPhreak User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 6 Days 21 h 32 m 25 sec
Reputation Power: 74
Quote:
Originally Posted by Scorpions4ever
Probably easier (but less efficient) to set all elements of b to zero initially and then simply copy a to b.

There is also another way. HINT: One of the standard C functions does this already.

SCORPY's PUZZLE OF THE WEEK: PM me the function name and you'll appear on my sig for a week.


Quote:
strncpy
Syntax:

#include <string.h>
char *strncpy( char *to, const char *from, size_t count );

The strncpy() function copies at most count characters of from to the string to. If from has less than count characters, the remainder is padded with '\0' characters. The return value is the resulting string.


Code:
strncpy(&b[0], &a[0], 64);
Comments on this post
kicken agrees!

Reply With Quote
  #6  
Old June 1st, 2006, 05:29 AM
arj arj is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 71 arj User rank is Lance Corporal (50 - 100 Reputation Level)arj User rank is Lance Corporal (50 - 100 Reputation Level)arj User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Day 2 h 48 m 28 sec
Reputation Power: 8
Quote:
Originally Posted by para45
Here's some pseudo-code. src is the array that you are copying from, and dest is the array you are copying to

Code:
for each character in src
   dest = assoicated src character
for the rest of the characters
   assoicated dest = 0




Here's exactly what it means:

#include<iostream>
using namespace std;

int main()
{
const int size1 = 3;//you can make
const int size2 = 6;//these dynamic given that size1<=size2
unsigned char src[size1];
unsigned char dest[size2]={'0'};

//initialize src array
for(int i=0; i<size1; i++)
cin>>src[i];

//Copy src to dest
for(int i=0; i<size1; i++)
dest[i] = src[i];

//OUTPUT dest:
for(int i=0; i<size2; i++)
cout<< dest[i]<<"\t";

return 0;
}
Comments on this post
jafet disagrees: Don't do his homework for him!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Copy char array

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap