473,387 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Copying a row of a two dimensional array into a one dimensional array

9
I am trying to string copy using strcpy, a row of a two dimensional array into a one dimensional array.

Can I do this?

Expand|Select|Wrap|Line Numbers
  1.     void function(char tempArray[][10])
  2. {
  3.     int i;
  4.      int j;
  5.      string minimum;
  6.  
  7.  
  8.      for(i=0; i< MAX_SIZE; i++)
  9.     {
  10.          location = i;
  11.          strcpy(minimum,tempArray[i]);
  12.      }
  13.  
  14. }
  15.  
  16.  
How about this?

Expand|Select|Wrap|Line Numbers
  1.  
  2.     void function(char tempArray[][10])
  3. {
  4.     int i;
  5.      int j;
  6.      char minimum[];
  7.  
  8.  
  9.      for(i=0; i< MAX_SIZE; i++)
  10.     {
  11.          location = i;
  12.          strcpy(minimum[],tempArray[i]);
  13.      }
  14.  
  15. }
  16.  
May 12 '10 #1
5 7605
v859
9
I didn't mean for that to be in a for loop.

I meant to say

Expand|Select|Wrap|Line Numbers
  1.  
  2. char tempArray[10][10];
  3. string minimum;
  4.  
  5. int i = 4;
  6. int location = i;
  7. strcpy(minimum, tempArray[i]);
  8.  
or

Expand|Select|Wrap|Line Numbers
  1.  
  2. char tempArray[10][10];
  3. char minimum[]
  4.  
  5. int i=4;
  6. strcpy(minimum,tempArray[i]);
  7.  
May 12 '10 #2
whodgson
542 512MB
What happens when you compile and run the code?
May 12 '10 #3
v859
9
I did get a can not convert char to char error message, but that is when I had

Expand|Select|Wrap|Line Numbers
  1.  
  2. char temporary;
  3. char array[10][10];
  4.  
  5. strcpy(temporary, array[10]);
  6.  
  7.  
Which is now obvious why I got that error, trying to put a string into a single char variable.

I don't know what happens when I try it the ways I listed above, since I do not have a C++ compiler on this computer.
May 12 '10 #4
weaknessforcats
9,208 Expert Mod 8TB
strcpy() starts copying from the address of the second argument and doesnt't stop until it hits a binary zero.

Therefore, using strcpy() in this fashion will never guarantee that 10 bytre are copied.

Is using strcpy() a homework requirement? If not, I sugget using strncpy() and tell it to copy only 10 bytes.

Since all arrays are one dimensional, you need only locate the starting address and copy 10 elements from there.
May 12 '10 #5
v859
9
I was able to solve those problems. Thanks for the help. I have a new problem though that I will post about in a new thread.
May 12 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Bob Bedford | last post by:
I've this structure in a XML file: Type Model Detail Datas Datas Datas Datas /Detail Detail
12
by: JKop | last post by:
template<class T> inline T& NthArrayMember( T & (array),size_t i) { return array; } template<class T> inline const T& NthArrayMember( const T & (array),size_t i) { return array;
4
by: Bob Bedford | last post by:
I've to save in an array of array some values. Problem: I don't know the size of the array before filling it. I'd like something like $bigarray = array(): then runnig the code I must...
7
by: Eric Boutin | last post by:
Hi ! I would like to generate an array of type char; I just dont really figure out how I could do it with malloc or calloc.. I mean.. I know how to allocate a simple array with both of them;...
10
by: Steve | last post by:
this code: private Array m_arrays = new Array; results in an array of 3 null Array objects. What am I missing?
4
by: Tad Marshall | last post by:
Hi, I'm reading about arrays in VB.NET and I seem to have a few options for my data structure. I need a multi-dimensional array of structures, and my first thought was Public Structure myStr...
3
by: valerio | last post by:
Hello all I would like to dinamically allocate an array of array of structures. To explain this: struct file{ char* fileName,int inode) myfiles; struct file{ char* fileName,int inode) mydirs; ...
2
by: Kenneth Brody | last post by:
Given the following current definition: int array; Is the following equivalent? (By "equivalent", I mean that any reference to "array" retains the same meaning, and that the memory layout...
8
by: =?big5?B?r0W84Q==?= | last post by:
Hi All C gurus: Below is a small program to print out the address of array and address of array variable: int main() { char array = "haha"; printf("array:%x\n", array); printf("&array:%x\n",...
4
by: Bob Bedford | last post by:
Hello, I've an array of array(1,2,3,4). I'd like to retrieve the values in 3 and 4 giving the values 1 and 2. How can I do that, how to search in an array of array ? in short, how to write the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.