473,326 Members | 2,133 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,326 software developers and data experts.

Copying two dimentional string array

7
Hello,

I have the following two dimensional string array (two_d_arr) which I would like to copy on to another string array (one_d_arr).

char *two_d_arr[][3] = { {"brown" , "yellow" , "red"} ,
{"apple" , "orange" , "lemon" },
{ "one" , "two" , "three"} };

char *one_d_arr;

int i;
for(i=0;i < 3; i++){
strncpy(one_d_arr[i],two_d_arr[1][i],strlen(one_d_arr[i])+1);
}

printf("%s\n", one_d_arr[0]);
printf("%s\n", one_d_arr[1]);
printf("%s\n", one_d_arr[2]);

I keep on getting the error:
warning: passing arg 1 of `strlen' makes pointer from integer without a cast

and segmentation fault.

Please help.
Jan 16 '09 #1
13 2285
JosAH
11,448 Expert 8TB
Variable one_d_arr is just a pointer to a char so one_d_arr[ i ] is a char that gets promoted to an int; that's what your compiler is complaining about: it expected a pointer (to a char). But more important: your one_d_arr doesn't point to anything so if you copy something to that location you'll copy it to nowhere and whammo: a segmentation fault can be all yours.

kind regards,

Jos
Jan 16 '09 #2
donbock
2,426 Expert 2GB
Do you mean that you want you program to convert this input:
Expand|Select|Wrap|Line Numbers
  1. char *two_d_arr[][3] = { {"brown" , "yellow" , "red"} ,
  2.                          {"apple" , "orange" , "lemon" },
  3.                          { "one" , "two" , "three"} };
To this output:
Expand|Select|Wrap|Line Numbers
  1. char *one_d_arr = "brownyellowredappleorangelemononetwothree"
I want to make sure I understand what you're trying to accomplish.

Take a look at http://bytes.com/topic/c/answers/859...elements-array
Jan 16 '09 #3
zak100
7
@donbock
I wanted to copy the following elements to one_d_arr:
{"apple" , "orange" , "lemon" }

so that :

*one_d_arr = {"apple" , "orange" , "lemon" };
Jan 16 '09 #4
donbock
2,426 Expert 2GB
You mean
Expand|Select|Wrap|Line Numbers
  1.  char *one_d_arr[] = {"apple", "orange", "lemon"};
Jan 16 '09 #5
zak100
7
Sorry. Yes. That's what I meant.

char *one_d_arr[] = {"apple", "orange", "lemon"};
Jan 16 '09 #6
donbock
2,426 Expert 2GB
What is the persistence of the input strings? If they are going to last awhile, then you don't need to copy the strings, you can simply copy the pointers. Try that and let us know how it works out.
Jan 16 '09 #7
zak100
7
How do I do that?

would that be like this?

int i;
for(i=0;i < 3; i++){
*one_d_arr[i] = *two_d_arr[1][i];
}
Jan 16 '09 #8
whodgson
542 512MB
Just to simplify the previous arguements and explanations the following code:
Expand|Select|Wrap|Line Numbers
  1.  
  2. char a[3][3]={{'a','b','c'},{'d','e','f'},{'g','h','k'}};
  3. char b[3]={0};
  4.  
  5.     for(int i=1,j=0;i=1,j<3;j++)
  6.       {
  7.        b[j]=a[i][j];
  8.        cout<<"b"<<j<<": "<<b[j]<<" ";
  9.        }
  10.  
..........prints ' b0: d b1: e b2: f
Is this what you are, in principle, trying to achieve?
It seems that your arrays are of type string not of type char.
Jan 18 '09 #9
zak100
7
I tried to run your code in C like this but got seg fault.

char a[3][3]={{'a','b','c'},{'d','e','f'},{'g','h','k'}};
char b[3]={0};

int i,j;
for(i=1,j=0;i=1,j<3;j++)
{
b[j]=a[i][j];
printf("B: %s is %s", j,b[j]);
}
Jan 18 '09 #10
JosAH
11,448 Expert 8TB
@zak100
Those matrix elements aren't strings here; they're chars so you should use a format specifier of %c, not %s; and j is an int.

kind regards,

Jos
Jan 18 '09 #11
zak100
7
The snippet now does what I wanted to do except that the arrays should be strings instead of chars.

char *a[3][3]={{"aaa","bbb","ccc"},{"ddd","eee","fff"},{"ggg"," hhh","kkk"}};
Jan 18 '09 #12
JosAH
11,448 Expert 8TB
@zak100
Well, then use that %s format specifier again for display purposes; the code snippet can stay the same.

kind regards,

Jos
Jan 18 '09 #13
zak100
7
Thanks. it works fine now.

char *a[3][3]={{"aaa","bbb","ccc"},{"ddd","eee","fff"},{"ggg", " hhh","kkk"}};
char *b[3]={0};

int i,j;
for(i=1,j=0;i=1,j<3;j++)
{
printf("B: %d is %s \n", j, a[1][j]);
}
Jan 19 '09 #14

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

Similar topics

2
by: Cedric Baelemans | last post by:
Hi, I try to store a two-dimentional array in a session variable. When I do the same with a one-dimentional array, it works fine, but with a two-dimentional array it fails. Is this not possible?...
2
by: Kurien Baker Fenn | last post by:
Can anybody tell me how to store data to a two dimentional array.if the array is declared as dim ArrayName(50,500). Thanks in advance
8
by: Al | last post by:
I'd like to declare (in a Matrix class) a two-dimentional array with user-defined coordinates. The constructor is: Matrix(int c, int l): col(c), lin(l), a(new float) {} the compiler says 'lin'...
1
by: rkmoray | last post by:
I have created a Multi Dimentional array, but having problems filling it. int mCount=0; mCount=ds.Tables.Rows.Count; string arrayppsa = new string ; DataTable myDataTable=ds.Tables;...
0
by: rkmoray | last post by:
I have created a Multi Dimentional array, but having problems filling it. int mCount=0; mCount=ds.Tables.Rows.Count; string arrayppsa = new string ; DataTable myDataTable=ds.Tables;...
0
by: Max M. Power | last post by:
I'm getting an InvalidOperationException calling Invoke with a method parameter object array that contains a two dimentional string array. More stack trace output below: // Create two...
14
by: ranjmis | last post by:
Hi all, Below is the code wherein I am initializing double dimentional array inside main with string literals. Now I want to display the strings using a function call to which I just want to...
2
by: Icarus27 | last post by:
Hello, I am making a program that runs off of a two dimentional array but I have made it into a game called MouseBot. I am trying to make the game so that you can move the mouse until you get the...
9
by: Srinu | last post by:
Hi All, We know the following facts, 1. A two dimentional arrays in C is stored similar to a single dimentional array. 2. * is the dereference operator that gives value at address denoted by...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.