Reputation: 209
How to copy row of 2d array to 1d array?
I was sure before I done it by assign like that:
int x,y;
scanf("%d%d", &x,&y);
int one[x];
int two[y][x];
one=two[0];
But now it gives me error :
incompatible types in assignment
EDIT:
Ok, my bad. I forgot that I can't copy arrays in such way, but I can pass single row of 2d array to function as an argument. Solved.
Upvotes: 1
Views: 9319
Reputation: 1
The below code may solve your problem:
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int i,row,col,col1;
char value1[10][4]={};
char *value2;
value2 = new char[40];
/** Creating a 2D char array ***/
for(i=0;i<8;i++)
{
strcpy(value1[i],"MK,");
}
value1[7][strlen(value1[7])-1]='\0';
/*** Printing the 2D array ****/
for(i=0;i<8;i++)
{
cout<<"\n\n value1[i]="<<value1[i];
}
/*** assigning the 2D array value1[][] to string value2 **/
col1=0;
for(row=0;value1[row][0]!='\0';row++)
{
for(col=0;value1[row][col]!='\0';col++)
{
*(value2+col1) = value1[row][col];
col1++;
}
}
*(value2+col1)='\0';
/** assignment done ***/
cout<<"\n\nSize of rows="<<row<<"\n\nSize of Column="<<col;
cout<<"\n\nvalue1="<<value1[0];
cout<<"\n\nValue2="<<value2;
return 0;
}
Upvotes: 0
Reputation: 2592
It seems that it isn't accepted ( I am not completely sure - I will look better ).
The semantics of the language are that for a unidimensional array the name of the array is a pointer to the type of element of the array and exactly a pointer to the first element. In your case the array is just a pointer of type (int*).
On any (int*) we may apply the [] brackets. These brackets as I have checked just translate to add the number inside the square brackets to the pointer ( using the pointer arithmetics ) and applying a dereference to the pointer. Explained in other words:
int *p;
p[nn] is equivalent to *(p+nn);
So in that vision having the code bellow:
int a[8],b[8];
a=b;
doing a=b could only copy the content of pointer b to the pointer a. But because the array is a constant pointer with no possibility to change that isn't possible. So the code above would report an error; Any way if it was possible it would copy the address where the array b starts to the pointer a. And that any way isn't what you need or what you expect.
That is the way that C treats arrays ( thanks for having obliged me to think on it ).
In the case the size was fixed I would do like that:
typedef struct{int arr[0x4];}S;
S a,b;
a=b;
In order to access the array you should use: a.arr[] and b.arr[]
In case you need an array of arrays it would be simple to extend to your case.
The problem is that in your case the size is dynamic.
The only simple solution is to use:
memcpy(a,b,sizeof(a));
That has the effect of copying b into a as you need. Every other solution is more complicated.
Upvotes: 0
Reputation: 619
Just use this code :
int col,rowToCopy=0;
for(col=0;col<x;col++){
one[col]=two[rowToCopy][col];
}
Upvotes: 1
Reputation: 106012
You can't do
one=two[0]; // Illegal assignment in C
Array names are non-modifiable l-values. It can't be a left operand of assignment operator =
. You can do this as
one[x] = two[y][x];
Upvotes: 3
Reputation: 386
You are trying to assign from a 2-dimensional array into 1-dimensional array! You will have to loop through the 2-d array and select each individual value and insert into 1-d array.
Upvotes: 3