Array of pointers

S

Slain

I am creating an array of char pointers.

char *str[2];
str[1]= "ma";

How ever the compiler gives me the following error. Any suggestions?

error: expected constructor, destructor, or type conversion before '='
token
error: expected `,' or `;' before '=' token
 
P

pauldepstein

I am creating an array of char pointers.

char *str[2];
str[1]= "ma";

How ever the compiler gives me the following error.  Any suggestions?

error: expected constructor, destructor, or type conversion before '='
token
error: expected `,' or `;' before '=' token

I was mystified by this error too. So I tested it using Visual c++
and Windows XP with a std::cout statement std::cout<<str[1]; for
testing
I got the string ma as expected.

In other words, no this does not give any error on my machine. But
you've obviously posted a snippet rather than the full code because,
for example, there's no main. Could you please reproduce code in its
entirety which generates the error?

Paul Epstein
 
S

Slain

I am creating an array of char pointers.
char *str[2];
str[1]= "ma";
How ever the compiler gives me the following error. Any suggestions?
error: expected constructor, destructor, or type conversion before '='
token
error: expected `,' or `;' before '=' token

I was mystified by this error too. So I tested it using Visual c++
and Windows XP with a std::cout statement std::cout<<str[1]; for
testing
I got the string ma as expected.

In other words, no this does not give any error on my machine. But
you've obviously posted a snippet rather than the full code because,
for example, there's no main. Could you please reproduce code in its
entirety which generates the error?

Paul Epstein

I am declaring this as a global variable outside main. I use this
later in a function

int x= size/ strlen(str);
for (int ii=0;ii<x; ii++)
{
strcat(String1,str);
}
 
S

Slain

I am creating an array of char pointers.
char *str[2];
str[1]= "ma";
How ever the compiler gives me the following error. Any suggestions?
error: expected constructor, destructor, or type conversion before '='
token
error: expected `,' or `;' before '=' token

I was mystified by this error too. So I tested it using Visual c++
and Windows XP with a std::cout statement std::cout<<str[1]; for
testing
I got the string ma as expected.

In other words, no this does not give any error on my machine. But
you've obviously posted a snippet rather than the full code because,
for example, there's no main. Could you please reproduce code in its
entirety which generates the error?

Paul Epstein

I am declaring this as a global variable outside main. I use this
later in a function

int x= size/ strlen(str);
for (int ii=0;ii<x; ii++)
{
strcat(String1,str);
}
 
S

Slain

I am creating an array of char pointers.
char *str[2];
str[1]= "ma";
How ever the compiler gives me the following error. Any suggestions?
error: expected constructor, destructor, or type conversion before '='
token
error: expected `,' or `;' before '=' token

I was mystified by this error too. So I tested it using Visual c++
and Windows XP with a std::cout statement std::cout<<str[1]; for
testing
I got the string ma as expected.

In other words, no this does not give any error on my machine. But
you've obviously posted a snippet rather than the full code because,
for example, there's no main. Could you please reproduce code in its
entirety which generates the error?

Paul Epstein

I am declaring this as a global variable outside main. I use this
later in a function

int x= size/ strlen(str);
for (int ii=0;ii<x; ii++)
{
strcat(String1,str);
}
 
J

Jack Klein

I am creating an array of char pointers.
char *str[2];
str[1]= "ma";

Now that you have finally given us the important information, below,
that is at namespace scope, we can tell you what the problem is.

The second line is an assignment, which is an executable statement.
Executable statements can only appear inside functions, not at file
scope.

You can initialize in a declaration at file scope:

char *str [2] = { "whatever", "ma" };

Otherwise, you can define the array without initialization, both
pointers will be set to NULL, and then change their contents with
assignments inside a function.
How ever the compiler gives me the following error. Any suggestions?
error: expected constructor, destructor, or type conversion before '='
token
error: expected `,' or `;' before '=' token

I was mystified by this error too. So I tested it using Visual c++
and Windows XP with a std::cout statement std::cout<<str[1]; for
testing
I got the string ma as expected.

In other words, no this does not give any error on my machine. But
you've obviously posted a snippet rather than the full code because,
for example, there's no main. Could you please reproduce code in its
entirety which generates the error?

Paul Epstein

I am declaring this as a global variable outside main. I use this
later in a function

int x= size/ strlen(str);
for (int ii=0;ii<x; ii++)
{
strcat(String1,str);
}

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
A

Alf P. Steinbach

* Slain:
I am creating an array of char pointers.
char *str[2];
str[1]= "ma";
How ever the compiler gives me the following error. Any suggestions?
error: expected constructor, destructor, or type conversion before '='
token
error: expected `,' or `;' before '=' token
I was mystified by this error too. So I tested it using Visual c++
and Windows XP with a std::cout statement std::cout<<str[1]; for
testing
I got the string ma as expected.

In other words, no this does not give any error on my machine. But
you've obviously posted a snippet rather than the full code because,
for example, there's no main. Could you please reproduce code in its
entirety which generates the error?

Paul Epstein

I am declaring this as a global variable outside main. I use this
later in a function

int x= size/ strlen(str);
for (int ii=0;ii<x; ii++)
{
strcat(String1,str);
}

Why don't you /read/ Paul's article, in particular the last paragraph,
that you responded to and quoted above?

Just to help you with this I'll quote the very last sentence again:



The code you posted first time was incomplete and sans relevant context.

The code you posted this time suffered from both those, plus was
inconsistent with the earlier code.

Third time,




Cheers, & hth.,

- Alf

PS: How to post a question about Code That Does Not Work As Expected is
a FAQ item. Please do read the FAQ. /Before/ posting.
 
S

Slain

* Slain:


I am creating an array of char pointers.
char *str[2];
str[1]= "ma";
How ever the compiler gives me the following error. Any suggestions?
error: expected constructor, destructor, or type conversion before '='
token
error: expected `,' or `;' before '=' token
I was mystified by this error too. So I tested it using Visual c++
and Windows XP with a std::cout statement std::cout<<str[1]; for
testing
I got the string ma as expected.
In other words, no this does not give any error on my machine. But
you've obviously posted a snippet rather than the full code because,
for example, there's no main. Could you please reproduce code in its
entirety which generates the error?
Paul Epstein
I am declaring this as a global variable outside main. I use this
later in a function
int x= size/ strlen(str);
for (int ii=0;ii<x; ii++)
{
strcat(String1,str);
}

Why don't you /read/ Paul's article, in particular the last paragraph,
that you responded to and quoted above?

Just to help you with this I'll quote the very last sentence again:

The code you posted first time was incomplete and sans relevant context.

The code you posted this time suffered from both those, plus was
inconsistent with the earlier code.

Third time,

Cheers, & hth.,

- Alf

PS: How to post a question about Code That Does Not Work As Expected is
a FAQ item. Please do read the FAQ. /Before/ posting.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Sorry Alf, This was my second time here I guess. I couldnt post the
whole code due to the big size. How ever I later did add the
information that it is a global variable. I guess thats the part I
should have added.

Thanks to jack, Hopefully this should work.
 
P

pauldepstein

I am creating an array of char pointers.
char *str[2];
str[1]= "ma";
How ever the compiler gives me the following error.  Any suggestions?
error: expected constructor, destructor, or type conversion before '='
token
error: expected `,' or `;' before '=' token
I was mystified by this error too.  So I tested it using Visual c++
and Windows XP with a std::cout statement  std::cout<<str[1]; for
testing
I got the string ma as expected.
In other words, no this does not give any error on my machine.  But
you've obviously posted a snippet rather than the full code because,
for example, there's no main.  Could you please reproduce code in its
entirety which generates the error?
Paul Epstein

I am declaring this as a global variable outside main. I use this
later in a function

int x= size/  strlen(str);
        for (int ii=0;ii<x; ii++)
        {
                 strcat(String1,str);
        }- Hide quoted text -

- Show quoted text -


strlen(str) doesn't make sense to me. strlen means stringlength but
str is not a string as you've defined it. I have no idea what you
mean by strlen(str);

Paul Epstein
 
S

Slain

* Slain:


I am creating an array of char pointers.
char *str[2];
str[1]= "ma";
How ever the compiler gives me the following error. Any suggestions?
error: expected constructor, destructor, or type conversion before '='
token
error: expected `,' or `;' before '=' token
I was mystified by this error too. So I tested it using Visual c++
and Windows XP with a std::cout statement std::cout<<str[1]; for
testing
I got the string ma as expected.
In other words, no this does not give any error on my machine. But
you've obviously posted a snippet rather than the full code because,
for example, there's no main. Could you please reproduce code in its
entirety which generates the error?
Paul Epstein
I am declaring this as a global variable outside main. I use this
later in a function
int x= size/ strlen(str);
for (int ii=0;ii<x; ii++)
{
strcat(String1,str);
}

Why don't you /read/ Paul's article, in particular the last paragraph,
that you responded to and quoted above?

Just to help you with this I'll quote the very last sentence again:

The code you posted first time was incomplete and sans relevant context.

The code you posted this time suffered from both those, plus was
inconsistent with the earlier code.

Third time,

Cheers, & hth.,

- Alf

PS: How to post a question about Code That Does Not Work As Expected is
a FAQ item. Please do read the FAQ. /Before/ posting.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Sorry Alf, This was my second time here I guess. I couldnt post the
whole code due to the big size. How ever I later did add the
information that it is a global variable. I guess thats the part I
should have added.

Thanks to jack, Hopefully this should work.
 
S

Slain

On Mar 4, 9:56 pm, (e-mail address removed) wrote:
I am creating an array of char pointers.
char *str[2];
str[1]= "ma";
How ever the compiler gives me the following error. Any suggestions?
error: expected constructor, destructor, or type conversion before '='
token
error: expected `,' or `;' before '=' token
I was mystified by this error too. So I tested it using Visual c++
and Windows XP with a std::cout statement std::cout<<str[1]; for
testing
I got the string ma as expected.
In other words, no this does not give any error on my machine. But
you've obviously posted a snippet rather than the full code because,
for example, there's no main. Could you please reproduce code in its
entirety which generates the error?
Paul Epstein
I am declaring this as a global variable outside main. I use this
later in a function
int x= size/ strlen(str);
for (int ii=0;ii<x; ii++)
{
strcat(String1,str);
}- Hide quoted text -
- Show quoted text -

strlen(str) doesn't make sense to me. strlen means stringlength but
str is not a string as you've defined it. I have no idea what you
mean by strlen(str);

Paul Epstein

Yes!!! That part of the code was before I tried to make str an array
if pointers. When I used the strlen I just used the str as a char
pointer.

Thanks
manny
 
I

Ian Collins

Slain said:
Yes!!! That part of the code was before I tried to make str an array
if pointers. When I used the strlen I just used the str as a char
pointer.
Is there a good reason why you can't use std::string?
 
P

pauldepstein

On Mar 4, 9:56 pm, (e-mail address removed) wrote:
I am creating an array of char pointers.
char *str[2];
str[1]= "ma";
How ever the compiler gives me the following error.  Any suggestions?
error: expected constructor, destructor, or type conversion before '='
token
error: expected `,' or `;' before '=' token
I was mystified by this error too.  So I tested it using Visual c++
and Windows XP with a std::cout statement  std::cout<<str[1]; for
testing
I got the string ma as expected.
In other words, no this does not give any error on my machine.  But
you've obviously posted a snippet rather than the full code because,
for example, there's no main.  Could you please reproduce code in its
entirety which generates the error?
Paul Epstein
I am declaring this as a global variable outside main. I use this
later in a function
int x= size/  strlen(str);
        for (int ii=0;ii<x; ii++)
        {
                 strcat(String1,str);
        }- Hide quoted text -
- Show quoted text -
strlen(str)  doesn't make sense to me.  strlen means stringlength but
str is not a string as you've defined it.  I have no idea what you
mean by strlen(str);
Paul Epstein

Yes!!! That part of the code was before I tried to make str an array
if pointers. When I used the strlen I just used the str as a char
pointer.

Thanks
manny- Hide quoted text -

- Show quoted text -

I don't understand why you say "Yes!!!" What question are you
answering "Yes!!!" to? It seems that "Yes!!!" should be replaced by
"Sorry for the confusion I caused."


Paul Epstein
 
F

faizulhaquezeya

I am creating an array of char pointers.

char *str[2];
str[1]= "ma";

How ever the compiler gives me the following error.  Any suggestions?

error: expected constructor, destructor, or type conversion before '='
token
error: expected `,' or `;' before '=' token

you are not allocating memory for that.
faiz ul haque zeya
Transys
 
I

Ian Collins

I am creating an array of char pointers.

char *str[2];
str[1]= "ma";

How ever the compiler gives me the following error. Any suggestions?

error: expected constructor, destructor, or type conversion before '='
token
error: expected `,' or `;' before '=' token

you are not allocating memory for that.

Allocating memory for what? The statement is a simple assignment, no
allocation is required.
 
S

Slain

I am creating an array of char pointers.
char *str[2];
str[1]= "ma";
How ever the compiler gives me the following error. Any suggestions?
error: expected constructor, destructor, or type conversion before '='
token
error: expected `,' or `;' before '=' token
you are not allocating memory for that.

Allocating memory for what? The statement is a simple assignment, no
allocation is required.

We do not have STL's

Paul:
By Yes, I was agreeing with you that code looks incorrect because I am
now using an array of pointers as opposed to a char pointer earlier.

And ofcurse apologies for the confusion :)

Thanks
Manny
 
P

pelio

Slain dixit:
I am creating an array of char pointers.
char *str[2];
str[1]= "ma";
How ever the compiler gives me the following error. Any suggestions?
error: expected constructor, destructor, or type conversion before '='
token
error: expected `,' or `;' before '=' token
I was mystified by this error too. So I tested it using Visual c++
and Windows XP with a std::cout statement std::cout<<str[1]; for
testing
I got the string ma as expected.

In other words, no this does not give any error on my machine. But
you've obviously posted a snippet rather than the full code because,
for example, there's no main. Could you please reproduce code in its
entirety which generates the error?

Paul Epstein

I am declaring this as a global variable outside main. I use this
later in a function


If your code is like that:

char *str[2]; // line 1
str[1] = "ma"; // line 2

// ...

int main()
{
}

be aware that line 2 is illegal.
 
Joined
Mar 9, 2008
Messages
4
Reaction score
0
man itsd not an array of pointers,
i know how and i will tell u but first what is an array???
for beginnrs they say its a group of elements for professionals they say an array is a constant pointer to the first memory location
anyway
i will show u how and it works properly
second thing u have to know is u use double quotes for strings and single quote of a character

so the result will be my friend :
#include<iostream.h>
#include<String.h>

int main()
{

char *str=new char[2];

str[0]='a';
str[1]='b';
cout<<str[0]<<endl;
cout<<str[1]<<endl;


return 0;
}
 
Joined
Mar 9, 2008
Messages
4
Reaction score
0
hi again i forgot to tell u if u want an array containing strings not character it will be like that:
#include<iostream.h>

#include<string.h>


int main()
{

char**str=new char*[3];

str[0]="welcome";
str[1]="byebye";


cout<<str[1]<<endl;
cout<<str[0]<<endl;
return 0;

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top