By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,132 Members | 1,967 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,132 IT Pros & Developers. It's quick & easy.

question about pointers in a 2-D array and strchr

P: 17
four strings are in a 2-D array; use strchr to find characters a, b, c, respectively;
search results are to be put in another 2-D arrray ter[ ][ ]:
results of searching 'a' in the 1st string go to ter[0][0], 'b' to ter[0][1]...
... 2nd string to to ter [1][0], 'b' to ter[1][1]...
...
here are the codes:
...
Expand|Select|Wrap|Line Numbers
  1.   int i, k;
  2.   char str[4][6]={"adevf", "dcdhn", "oledc", "brdca"};
  3.   char chr[3]={'a', 'b', 'c'};
  4.   char ter[i][3];
  5.  
  6.   for (i=0; i<4; i++)
  7.     {
  8.        for (k=0; j<3; k++);
  9.          {
  10.                &ter[i][k] = strchr(str[i], chr[k]);
  11.          }
  12.     }
...
when compiling, line 10 is "invalid lvalue in assignment".
when using (ter[i]+k) = strchr(str[i], chr[k]); - same error.

So I guess the used the wrong pointer. Actually, I was confused.
What is wrong?
Jun 5 '07 #1
Share this Question
Share on Google+
10 Replies


Savage
Expert 100+
P: 1,764

Expand|Select|Wrap|Line Numbers
  1.  char str[4][6]={"adevf", "dcdhn", "oledc", "brdca"}; 
  2.   int i;
  3.   char chr[3]={'a', 'b', 'c'};
  4.   char ter[i][3].  .
  5.  
  6.  for (i=0; i<4; i++).   {
  7.  
  8.       for (k=0; j<3; k++);
  9.         {
  10.              &ter[i][k] = strchr(str[i], chr[k]);
  11.          }
  12.     }
  13. ...
when compiling, line 10 is "invalid lvalue in assignment".
when using (ter[i]+k) = strchr(str[i], chr[k]); - same error.

So I guess the used the wrong pointer. Actually, I was confused.
What is wrong?
Don't u get error because i is not constant?(Bolded lines in ur code)

Now to the point:

Because two dimensinal arrays doesn't really exist this code:

char ter[i][3] is same as char ter[3*i],so think about this..

Savage
Jun 5 '07 #2

AdrianH
Expert 100+
P: 1,251
Don't u get error because i is not constant?(Bolded lines in ur code)

Now to the point:

Because two dimensinal arrays doesn't really exist this code:

char ter[i][3] is same as char ter[3*i],so think about this..

Savage
This may work on some compilers. The C99 standard allows for it, and some others have it as an extention. However, i is not initialised so a warning should still show up. Also, don't use bold, it doesn't show up very well with this font.

As for the OP's original question. What do you want line 10 to do?


Adrian
Jun 5 '07 #3

Savage
Expert 100+
P: 1,764
...don't use bold, it doesn't show up very well


Adrian

OK,thanks.

Savage
Jun 5 '07 #4

P: 17
This may work on some compilers. The C99 standard allows for it, and some others have it as an extention. However, i is not initialised so a warning should still show up. Also, don't use bold, it doesn't show up very well with this font.

As for the OP's original question. What do you want line 10 to do?


Adrian
Thanks!
line 10 gets the results of strchr, putting them in that two dimensional array. I think the problem is the pointer in line 10. But I don't know how to solve it.
I think i is initialized, see line 1. (is it right?)
Jun 5 '07 #5

Savage
Expert 100+
P: 1,764
Thanks!
line 10 gets the results of strchr, putting them in that two dimensional array. I think the problem is the pointer in line 10. But I don't know how to solve it.
I think i is initialized, see line 1. (is it right?)
No,it is not initialized.It contains random value.Initialized variable has a default value not a random,so this:

int i=0;

is a example of initialized value.

Line 10,tells the compiler to put value returned from strchr in adress of ter,which is not corrrect.



Savage
Jun 5 '07 #6

AdrianH
Expert 100+
P: 1,251
Perhaps you intended to declare ter as a 2d array of char pointers? Like this: char* ter[i][3];?


Adrian
Jun 6 '07 #7

P: 17
With the help and hints I got from here, I figure it out!
The key is array of pointers needs to be used instead of pointer to array.
I tested the following code. It works. Note that it is not necessary to initiate int i=0;
Many thanks to Savage and AdrianH!

Expand|Select|Wrap|Line Numbers
  1.  int i, k;
  2.  char str[4][6]={"adevf", "dcdhn", "oledc", "brdca"};
  3.  char chr[3]={'a', 'b', 'c'};
  4.  char *ter[4][3] = {NULL};
  5.  
  6.  for (i=0; i<4; i++)
  7.     {
  8.      for (k=0; j<3; k++);
  9.         {
  10.            ter[i][k] = strchr(str[i], chr[k]);
  11.        }
  12.     }
  13.                      ...
Jun 6 '07 #8

AdrianH
Expert 100+
P: 1,251
With the help and hints I got from here, I figure it out!
The key is array of pointers needs to be used instead of pointer to array.
I tested the following code. It works. Note that it is not necessary to initiate int i=0;
Many thanks to Savage and AdrianH!

Expand|Select|Wrap|Line Numbers
  1.  int i, k;
  2.  char str[4][6]={"adevf", "dcdhn", "oledc", "brdca"};
  3.  char chr[3]={'a', 'b', 'c'};
  4.  char *ter[4][3] = {NULL};
  5.  for (i=0; i<4; i++)
  6.     {
  7.      for (k=0; j<3; k++);
  8.         {
  9.            ter[i][k] = strchr(str[i], chr[k]);
  10.        }
  11.     }
...
It is only not necessary to initialise i becasue i is initialised on line 6 prior to use. Please use [code=c][/code] or [code=cpp][/code] tags instead of numbering them yourself. It is easier to read.


Adrian
Jun 6 '07 #9

Savage
Expert 100+
P: 1,764
It is only not necessary to initialise i becasue i is initialised on line 6 prior to use. Please use [code=c][/code] or [code=cpp][/code] tags instead of numbering them yourself. It is easier to read.


Adrian
Adrian,now look what ur crack has done to u:

i is initialized at line 5 not 6.

Tick,tick,tick..

U really,really should get of that crack.

Savage
Jun 6 '07 #10

AdrianH
Expert 100+
P: 1,251
Adrian,now look what ur crack has done to u:

i is initialized at line 5 not 6.

Tick,tick,tick..

U really,really should get of that crack.

Savage
It is all that inferior stuff you give me. :D


Adrian
Jun 6 '07 #11

Post your reply

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