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.
13 2285
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
Do you mean that you want you program to convert this input: - char *two_d_arr[][3] = { {"brown" , "yellow" , "red"} ,
-
{"apple" , "orange" , "lemon" },
-
{ "one" , "two" , "three"} };
To this output: - 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 @donbock
I wanted to copy the following elements to one_d_arr:
{"apple" , "orange" , "lemon" }
so that :
*one_d_arr = {"apple" , "orange" , "lemon" };
You mean - char *one_d_arr[] = {"apple", "orange", "lemon"};
Sorry. Yes. That's what I meant.
char *one_d_arr[] = {"apple", "orange", "lemon"};
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.
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];
}
Just to simplify the previous arguements and explanations the following code: -
-
char a[3][3]={{'a','b','c'},{'d','e','f'},{'g','h','k'}};
-
char b[3]={0};
-
-
for(int i=1,j=0;i=1,j<3;j++)
-
{
-
b[j]=a[i][j];
-
cout<<"b"<<j<<": "<<b[j]<<" ";
-
}
-
..........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.
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]);
}
@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
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"}};
@zak100
Well, then use that %s format specifier again for display purposes; the code snippet can stay the same.
kind regards,
Jos
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]);
}
Sign in to post your reply or Sign up for a free account.
Similar topics
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?...
|
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
|
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'...
|
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;...
|
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;...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
| |