473,387 Members | 3,033 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.

question about pointers in a 2-D array and strchr

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
10 1852
Savage
1,764 Expert 1GB

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
1,251 Expert 1GB
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
1,764 Expert 1GB
...don't use bold, it doesn't show up very well


Adrian

OK,thanks.

Savage
Jun 5 '07 #4
runsun
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
1,764 Expert 1GB
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
1,251 Expert 1GB
Perhaps you intended to declare ter as a 2d array of char pointers? Like this: char* ter[i][3];?


Adrian
Jun 6 '07 #7
runsun
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
1,251 Expert 1GB
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
1,764 Expert 1GB
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
1,251 Expert 1GB
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

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

Similar topics

10
by: MathStudent | last post by:
Can someone take a look at my code and let me know if you see any mistakes? ======================== #include <stdio.h> #include <ctype.h> #include <conio.h> // Used to make the getch() work...
7
by: Eddahbi Karim | last post by:
Hi, I have a pointer *pos, an array of structure "valid_param_s" and a member name. (void) fprintf(stderr, "Invalid parameter %s\n" "Type --help for more " "informations\n"...
27
by: glen herrmannsfeldt | last post by:
The subject recently came up in comp.compilers, though I believe that I asked here before. If you use relational operators, other than == and !=, on pointers to different objects, is there any...
5
by: jimjim | last post by:
Hello, Any help will be much appreciatted. My problem is as follows: I declare as global variables: typedef struct _Node{ ..; ..;}Node; Node *Graph; in a function called initiallise(), I...
12
by: Rob Morris | last post by:
Hi, I'm teaching myself C for fun. I wrote the litle program listed below to convert rot13 text. It reads one char at a time and converts it via pointers. The constant char* letters holds the...
6
by: mann! | last post by:
hi can some one please explain how int (*x) declares a pointer to an array and int *x declares an array of pointers?
11
by: Seven Kast USA | last post by:
hi What is use of pointerrs in c programming.is fasster than other types by KAST
22
by: walter.preuninger | last post by:
Is there an easier way to code the cmp procedure without going thru all the pointer manipulations? #include <stdlib.h> #include <string.h> int cmp(const void *i, const void *j) { void *p1,...
6
by: Computer Wizard | last post by:
Hello, I am really scrwed up with the following stuff: struct employee { char* employee_name; }; struct department {
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.