473,656 Members | 2,777 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PROBLEM: why can't auto-covert char [10][10] into char **

Hi All,

char [10][10] to char ** is compile error. why?

if i hava a string array. yes. it's not safe and it's better to use
vector<stringin stead.

but my point is the language feature.

char sex[2][128] = {"Male", "Female" };

void print(char **p, int len) {
// print all sex
}

print(sex, 128); // <--- this cause compile error.

Is any issue to make the language forbiden this covertion?

Regards
-Wisdo
Aug 18 '06 #1
21 2291

void print(char (*p)[128], int len)

definition should fix your problem. Functions that have 2 dimensional
arrays
as arguments need a hint to be able to offset the arrays properly. This
is
because these are arrays of arrays actually.

Hope this helps,

Tolga Ceylan

Aug 18 '06 #2
Wisdo <wi***@hf.webex .comwrote:
Hi All,

char [10][10] to char ** is compile error. why?

if i hava a string array. yes. it's not safe and it's better to use
vector<stringin stead.

but my point is the language feature.

char sex[2][128] = {"Male", "Female" };

void print(char **p, int len) {
// print all sex
}

print(sex, 128); // <--- this cause compile error.

Is any issue to make the language forbiden this covertion?
Yes, char[][] is not the same as a char**. 'sex' isn't a pointer to a
pointer to a char.
Aug 18 '06 #3

"Wisdo" <wi***@hf.webex .comwrote in message
news:ec******** **@news.yaako.c om...
Hi All,

char [10][10] to char ** is compile error. why?

if i hava a string array. yes. it's not safe and it's better to use
vector<stringin stead.

but my point is the language feature.

char sex[2][128] = {"Male", "Female" };

void print(char **p, int len) {
// print all sex
}

print(sex, 128); // <--- this cause compile error.

Is any issue to make the language forbiden this covertion?

Regards
-Wisdo
Because char [10][10] is not an array of pointers, it is a two dimentional
array of characters.
The memory is allocated and used about the same way as char [100] and you
can test that yourself.

This prints out Male and Female twice. Pick the one you like best.

void print( const char p[][128], const int length )
{
for ( int i = 0; i < length; ++i )
std::cout << &p[i][0] << std::endl;
}

void print2( const char* p, const int width, const int length )
{
for ( int i = 0; i < length; ++i )
std::cout << &p[width * i] << std::endl;
}

int main()
{
char sex[2][128] = {"Male", "Female" };
print( sex, 2 );
print2( reinterpret_cas t<const char *>( sex ), 128, 2 );
}
Aug 18 '06 #4
Wisdo posted:
Hi All,

char [10][10] to char ** is compile error. why?

Type mismatch, both before and after the array-to-pointer decay.

if i hava a string array. yes. it's not safe and it's better to use
vector<stringin stead.

Incorrect -- arrays yield well-defined behaviour in C++.

void print(char **p, int len) {
// print all sex
}

print(sex, 128); // <--- this cause compile error.

Is any issue to make the language forbiden this covertion?

Your speech is unintelligible.

Here's some sample code:

#include <cstddef>
#include <cassert>
#include <iostream>

using std::cout;
using std::size_t;

#define restrict /* Nothing */
#define nullptr 0 /* Until next standard */

size_t const buflen = 64;

void PrintNullTermin atedArrayOfPoin tersToStrings(c har const *const restrict
*restrict p)
{
assert(p);
assert(*p);

do cout << *p++ << '\n';
while(*p);
}

void PrintNullTermin atedArrayOfStri ngs(char const *restrict p)
{
assert(p);
assert(*p);

do cout << p << '\n';
while(*(p += buflen));
}

int main()
{
char const *const restrict names1[] = {
"Michael","John ","Philip","Bar ry","Thomas",
"Luke","Owen"," Richard","Keith ",nullptr};

char const names2[][buflen] = {
"Michael","John ","Philip","Bar ry","Thomas",
"Luke","Owen"," Richard","Keith ", {0} };

PrintNullTermin atedArrayOfPoin tersToStrings(n ames1);

PrintNullTermin atedArrayOfStri ngs(*names2);
}

--

Frederick Gotham
Aug 18 '06 #5
In article <ec**********@n ews.yaako.com>, Wisdo <wi***@hf.webex .comwrote:
>Hi All,

char [10][10] to char ** is compile error. why?

if i hava a string array. yes. it's not safe and it's better to use
vector<stringi nstead.

but my point is the language feature.

char sex[2][128] = {"Male", "Female" };

void print(char **p, int len) {
// print all sex
}

print(sex, 128); // <--- this cause compile error.

Is any issue to make the language forbiden this covertion?
The problem is that you do not have an array of pointers,
you have an array or array's, so something like sex[1][2]
has to get to the right group of [128]'s. It's not sure
what you want here, but perhaps you want to pass each
sex[i] and accept a char * instead.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 18 '06 #6
In article <ec**********@p anix2.panix.com >,
Greg Comeau <co****@comeauc omputing.comwro te:
>In article <ec**********@n ews.yaako.com>, Wisdo <wi***@hf.webex .comwrote:
>>char [10][10] to char ** is compile error. why?

if i hava a string array. yes. it's not safe and it's better to use
vector<string instead.

but my point is the language feature.

char sex[2][128] = {"Male", "Female" };

void print(char **p, int len) {
// print all sex
}

print(sex, 128); // <--- this cause compile error.

Is any issue to make the language forbiden this covertion?

The problem is that you do not have an array of pointers,
you have an array or array's, so something like sex[1][2]
has to get to the right group of [128]'s. It's not sure
what you want here, but perhaps you want to pass each
sex[i] and accept a char * instead.
Oops if not obvious "array or arrays" should say "array of arrays"
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 18 '06 #7

to***********@y ahoo.com wrote:
void print(char (*p)[128], int len)
Yet another reason to just use the standard classes to your advantage
is that rather convoluted and entirely necissary definition.

Aug 18 '06 #8
Wisdo wrote:
Hi All,

char [10][10] to char ** is compile error. why?

if i hava a string array. yes. it's not safe and it's better to use
vector<stringin stead.

but my point is the language feature.

char sex[2][128] = {"Male", "Female" };

void print(char **p, int len) {
// print all sex
}

print(sex, 128); // <--- this cause compile error.

Is any issue to make the language forbiden this covertion?

Regards
-Wisdo
Simply, one (IMHO) convenient way to implement the print function is to
use template:

#include <iostream>

using namespace std;

template <size_t N>
void print(char p[][N], int len)
{
for(int i = 0 ; i < len ; ++i)
{
cout << p[i] << endl;
}
}

int main()
{
char sex[2][128] = {"Male", "Female" };
print(sex, 2);
return 0;
}

Pierre
Aug 18 '06 #9
Noah Roberts posted:
>void print(char (*p)[128], int len)

Yet another reason to just use the standard classes to your advantage
is that rather convoluted and entirely necissary definition.

I see nothing convoluted about it, but then again I'm not afraid of C++.

--

Frederick Gotham
Aug 18 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3213
by: Chris Online | last post by:
Hi all, I'm using C++ Builder5. I want to get data from an edit-box and send it to a development kit. The dev-kit can only receive char and no char* here's a part of my code: char* Data_byte = 0x00; UCHAR outBuffer;
30
5039
by: Tim Johansson | last post by:
I'm new to C++, and tried to start making a script that will shuffle an array. Can someone please tell me what's wrong? #include <iostream.h> #include <string.h> int main () { srand(time(0)); int array_length; int count;
5
4544
by: spoilsport | last post by:
Ive got to write a multi-function program and I'm plugging in the functions but I keep getting this error from line 40. Im new to this and cant find an answer anywhere. Sam #include <stdio.h> int main (void)
12
3305
by: Lars Langer | last post by:
Hi there, I'm new to this C - never the less - I'm trying to search a string for the occurence of a substring - However, I'm not very succesful since my use of the strstr function always returns NULL. But I know that there exsists such a substring, as I can see it with my own two eye when I print out the string to be searched. I added the code of the function (resolveResponse)and how I call this code - Calling the function:
12
10078
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display it. here is the 4 errors. c:\C++\Ch15\Employee.h(29): error C2440: '=' : cannot convert from 'char ' to 'char '
3
2947
by: aldonnelley | last post by:
Hi there. I'm just learning c++, and this is driving me nuts. I'm trying to save image files generated in a for loop with a filename built using strcat with: - a char base file name + a character to identify the individual files that is indexed from a char array by the int index of the for loop + a char file extension.
34
31282
by: Perro Flaco | last post by:
Hi! I've got this: string str1; char * str2; .... str1 = "whatever"; .... str2 = (char *)str1.c_str();
4
2151
by: Xavier Roche | last post by:
Hi folks, I have a probably rather silly question: is casting a char array in a char* a potential source of aliasing bug ? Example: a fonction returning a buffer taken in a circular buffer typedef struct foo_t foo_t; struct foo_t { int index;
6
1770
Nepomuk
by: Nepomuk | last post by:
Hi there! I was trying something with strings in C++ and ran into a problem. I wanted to write a program to do the following task: Read two "string" inputs and combine them into a third "string". Now, in the book I'm using, only char arrays and char pointers were used so far, so I wanted to solve the task with that. I came up with the following solution:#include <iostream> #include <cstring> int main() { char a; char b;
5
5385
by: slizorn | last post by:
well the error i get is the title above: error C2664: 'searchTree' : cannot convert parameter 2 from 'const char *' to 'char' error is form this line searchTree(treeObj->root ,data1.c_str()); i have also attached searchTree below for yr reference
0
8382
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
8297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
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...
1
8498
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7311
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5629
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
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1600
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.