syntax problem

M

muser

I'am having problem with the following:

rec1len = strlen(temp1);

temp1 is a character array, multi dimensional array.
it has been initialised as char temp1[12][104];
int rec1len.
error for this message: C:\Program Files\Microsoft Visual
Studio\MyProjects\Valid\Zenith124\Zenith.cpp(162) : error C2664:
'strlen' : cannot convert parameter 1 from 'char [12][104]' to 'const
char *'

can anyone help please.
 
M

MiniDisc_2k2

muser said:
I'am having problem with the following:

rec1len = strlen(temp1);

temp1 is a character array, multi dimensional array.
it has been initialised as char temp1[12][104];
int rec1len.
error for this message: C:\Program Files\Microsoft Visual
Studio\MyProjects\Valid\Zenith124\Zenith.cpp(162) : error C2664:
'strlen' : cannot convert parameter 1 from 'char [12][104]' to 'const
char *'

can anyone help please.

That's not how strlen() works. strlen() works with char*s, not character
arrays.

If you had something like this:

char temp1[12];

It would work if you took the address of that:

rec1len = strlen(&temp1);

But because you have a multidimensional array, you cannot do that. What I'm
assuming you're doing is having 104 "strings" which are up to 12 characters
long (or 12 strings up to 104 characters long). Note I say string here
meaning the C style string, that is, a char*. Perfectly legal in C++ but a
little bit harder to work with. If you must keep it char[12][104] and just
wanted to add up all of the string lengths, here's what you'll want to do:

rec1len = 0;
for (int x = 0; x < 104; x++)
rec1len += strlen(&temp1[x]);

I'm pretty sure that's how it works. I may have the two indexes mixed up,
but you're only supposed to put one of them down. I think it's the second
one, and then take the address of the first. Anyone else there please feel
free to pipe in if I got it wrong.

BTW: In C++, we have an easier way of dealing with strings, the aptly named
string class. First off, you'll need to

#include <string>
using namespace std; // if you haven't already

string temp1[104]; // make 104 strings

And now to add up the lengths, you could

rec1len = 0;
for (int x = 0; x < 104; x++)
rec1len += temp1[x].length();


Code Not Compiled
 
J

John Carson

muser said:
I'am having problem with the following:

rec1len = strlen(temp1);

temp1 is a character array, multi dimensional array.
it has been initialised as char temp1[12][104];
int rec1len.
error for this message: C:\Program Files\Microsoft Visual
Studio\MyProjects\Valid\Zenith124\Zenith.cpp(162) : error C2664:
'strlen' : cannot convert parameter 1 from 'char [12][104]' to 'const
char *'

can anyone help please.

Not sure what you are trying to do. strlen is not meant to work with
multi-dimensional arrays. You can call

strlen(temp1)

to get the length of row i for any i = 0 to 11. But this will only work if
the row is nul terminated. strlen counts the number of characters before the
'\0' character is encountered.
 
B

Bob Jacobs

muser said:
I'am having problem with the following:

rec1len = strlen(temp1);

temp1 is a character array,

It's an array of arrays in fact.
multi dimensional array.
it has been initialised as char temp1[12][104];
int rec1len.
error for this message: C:\Program Files\Microsoft Visual
Studio\MyProjects\Valid\Zenith124\Zenith.cpp(162) : error C2664:
'strlen' : cannot convert parameter 1 from 'char [12][104]' to 'const
char *'

can anyone help please.

What is it that you're intending with strlen(temp1)? It's not 100% clear to
me.

If you want string length, which string do you want the length of? You have
an array of strings so identify which one you want, for instance:

int rec1len = strlen(temp[0]);

for the first string, or:

int rec12len = strlen(temp[11]);

for the last string.
 
J

John Harrison

muser said:
I'am having problem with the following:

rec1len = strlen(temp1);

temp1 is a character array, multi dimensional array.
it has been initialised as char temp1[12][104];

declared as char temp1[12][104];, there's no initialisation going on there.
int rec1len.
error for this message: C:\Program Files\Microsoft Visual
Studio\MyProjects\Valid\Zenith124\Zenith.cpp(162) : error C2664:
'strlen' : cannot convert parameter 1 from 'char [12][104]' to 'const
char *'

can anyone help please.

What did you expect to happen? strlen is for strings, i.e. *one* dimensional
character arrays. If you explain what you are trying to do someone will be
able to help.

john
 

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
474,113
Messages
2,570,690
Members
47,269
Latest member
VitoYwo03

Latest Threads

Top