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:
... - int i, k;
-
char str[4][6]={"adevf", "dcdhn", "oledc", "brdca"};
-
char chr[3]={'a', 'b', 'c'};
-
char ter[i][3];
-
-
for (i=0; i<4; i++)
-
{
-
for (k=0; j<3; k++);
-
{
-
&ter[i][k] = strchr(str[i], chr[k]);
-
}
-
}
...
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?
10 1883 Savage 1,764
Recognized Expert Top Contributor - char str[4][6]={"adevf", "dcdhn", "oledc", "brdca"};
-
int i;
-
char chr[3]={'a', 'b', 'c'};
-
char ter[i][3]. .
-
-
for (i=0; i<4; i++). {
-
-
for (k=0; j<3; k++);
-
{
-
&ter[i][k] = strchr(str[i], chr[k]);
-
}
-
}
-
...
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
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
Savage 1,764
Recognized Expert Top Contributor
...don't use bold, it doesn't show up very well
Adrian
OK,thanks.
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
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?)
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
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
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! - int i, k;
-
char str[4][6]={"adevf", "dcdhn", "oledc", "brdca"};
-
char chr[3]={'a', 'b', 'c'};
-
char *ter[4][3] = {NULL};
-
-
for (i=0; i<4; i++)
-
{
-
for (k=0; j<3; k++);
-
{
-
ter[i][k] = strchr(str[i], chr[k]);
-
}
-
}
-
...
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! - int i, k;
-
char str[4][6]={"adevf", "dcdhn", "oledc", "brdca"};
-
char chr[3]={'a', 'b', 'c'};
-
char *ter[4][3] = {NULL};
-
for (i=0; i<4; i++)
-
{
-
for (k=0; j<3; k++);
-
{
-
ter[i][k] = strchr(str[i], chr[k]);
-
}
-
}
...
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
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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>
|
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);
|
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() {
|
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 )
|
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...
| |
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?
|
by: Seven Kast USA |
last post by:
hi
What is use of pointerrs in c programming.is fasster than other types
by
KAST
|
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;
|
by: Computer Wizard |
last post by:
Hello,
I am really scrwed up with the following stuff:
struct employee
{
char* employee_name;
};
struct department
{
|
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...
|
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...
| |
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |