473,657 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question about pointers in a 2-D array and strchr

17 New Member
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 1883
Savage
1,764 Recognized Expert Top Contributor

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?(Bolde d 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 Recognized Expert Top Contributor
Don't u get error because i is not constant?(Bolde d 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 Recognized Expert Top Contributor
...don't use bold, it doesn't show up very well


Adrian

OK,thanks.

Savage
Jun 5 '07 #4
runsun
17 New Member
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 Recognized Expert Top Contributor
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.Initializ ed 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 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Top Contributor
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 Recognized Expert Top Contributor
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

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

Similar topics

10
3594
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 #include <string.h>
7
5846
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" ,valid_params_s.name);
27
3116
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 requirement on the result? #include <stdio.h> int main() {
5
2844
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 allocate memory and copy information: for(i )
12
1347
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 alphabet. I subract the pointer returned from strchr from the address of letters to get the location within the alphabet, then rot13 it. My question is, is this safe and legal? (It works on my windows machine BTW.) I googled for programs to do...
6
13705
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
429
by: Seven Kast USA | last post by:
hi What is use of pointerrs in c programming.is fasster than other types by KAST
22
1942
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, *p2; char **s1, **s2;
6
5874
by: Computer Wizard | last post by:
Hello, I am really scrwed up with the following stuff: struct employee { char* employee_name; }; struct department {
0
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8730
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1607
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.