Hi, I would like to dynamic assigning different sets of strings to display on screen, such as,
if user choose 1, display "1111", "111111", "111",
if user choose 2, display "222", "22222", "2222", "22",
if user chooses 3, then "333", "33"
Is the following declaration OK?
char *str[][3]={{"1111", "111111", "111"}, {"222", "22222", "2222", "22"}, {"333", "33"}};
seems compiler always waring that
warning: initialization from incompatible pointer type
warning: excess elements in scalar initializer
how could I solve this problem? thanks in advance
4 3624
If I take the above declaration as such then the obvious error is that the declaration of the str size is wrong. You have taken strings that are having around 6 characters whereas in the declaration you have declared the size as 3. This is why the compiler is throwing errors.
If this is not what you wanted to know then pls elobrate your question becoz what I understood was this. One more thing, its working fine on my system.
Banfa 9,065
Recognized Expert Moderator Expert
Not quite right Ashish_CPP, there is no string size problem because the actual data type is char *, but you are along the right lines.
The problem with
char *str[][3]={{"1111", "111111", "111"}, {"222", "22222", "2222", "22"}, {"333", "33"}};
is that you have delared an unspecified size array to an array of 3 char *, however in the 22222 structure you have not 3 but 4 strings (pointers to char *) and since 4 > 3 you get
warning: excess elements in scalar initializer
This can probably be fixed by changing the declaration to
char *str[][4]={{"1111", "111111", "111"}, {"222", "22222", "2222", "22"}, {"333", "33"}};
however this has a small gotcha because the 1st and 3rd entries in the array still contain 4 pointers but you have only provided 3 and 2 initialisers respectively. The entries that you have not provided initialisers for (str[0][3] for instance) will have the value NULL.
Banfa 9,065
Recognized Expert Moderator Expert
warning: initialization from incompatible pointer type
It is possible that on your system string constants are of type const char *, this is sometimes true in which case char * and const char * are not the same causing this message.
thanks! but what if in the following case?
there are conditions that might have different selections for user, such as
char *format_1[]={"aaaa", "bbbb", "cc", "ddd"};
char *format_2[]={"111", "2222", "33333", "444", "5555"};
..
if user choose 1, I have to count how many items in the string list and show the contents of format_1; 2, list format_2... etc.
This is a simple example which can be achieved by 1 if-else statement, but what if there are 10 cases?
can I just use an array to save these strings and do things just like getting array entries from array index to get the whole format_1 or so?
Is there any better way to declare such cases?
Thanks for any kindly answer and help... :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: gtz669 |
last post by:
I have a class which I am using for data stroage. I declare an
instance of that class in my main class which is running my java
applet. I Iassign it a value in the init () function and it works
fine but when I try to do the same assignment later on, it restults in
a NullPointerException and I can not figure this out. Could someone
please help. Thanks.
Here's the code:
public class A extends java.applet.Applet
|
by: Jack |
last post by:
Hi,
I am trying to test a sql statement in Access which gives me
the error as stated in the heading.
The sql statement is built as a part of asp login verification,
where the userid and password are input in login screen.
The password in the database is a number field.
I am writing the dynamic sql statement as follows below. I believe
I am going wrong in the password section of the code. I
appreciate any help. Thanks. Regards.
|
by: drj0nson |
last post by:
What is the right way of creating a string/char array and assigning to
a char* which is then used in a function call. Thought it would be
quite nice to avoid a memory leakage /core dump.
1 Header with char* x
2 Class includes header
3 In Class member function I want to :
a)conditionally create a string ie populate x
|
by: Greg |
last post by:
I am able to read an html file and display the text in a textbox, no
problem:
Dim sr As New StreamReader(Server.MapPath("myfile.htm")
Dim str as string = sr.ReadToEnd
txtBox.Text = str
Once in the textbox, the user can edit this standard template before
saving it to a database.
|
by: Shiju Poyilil |
last post by:
I have a link button "lButton" created dynamically at the item databound
event of the data grid "datagrid1" in the footer.so i want to execute the
item command event of the datagrid on clicking the dynamicaly created link
button, but when at runtime i click the dymanic created control it disapears
from the datagrid footer and then it doesnt fire the itemcommand event of the
datagrid.
I am pasting below the pieces of my code for your...
| |
by: Shelby |
last post by:
Hi,
if I have :
rs.Fields("firstname").Value = "John"
rs.Fields("middlename").Value = "D"
rs.Fields("lastname").Value = "Paul"
Is it possible to Dim the column name and make it a variable and reassign
the value to this new variable? Because I have a long list of the above, and
I wish to loop thru it, instead of assigning it one by one.
|
by: Russ Chinoy |
last post by:
Hi,
This may be a totally newbie question, but I'm stumped.
If I have a function such as:
function DoSomething(strVarName) {
.....
}
|
by: Paul |
last post by:
Hi all,
I have a string which I want to break into a char array, is the
following way of doing this ok:
const char* test = new char;
test = sQuery.c_str();
and finally:
|
by: arnuld |
last post by:
I have created a program to print the input words on stdout. Input is
taken dynamically from stdin. In each word, each input character is
allocated dynamically. I have ran this program with a file containing a
*single* word made of 25525500 letters and this program works fine on it.
I will welcome any suggestions for improvement.
/* * A program that will ask the user for input and then will print the words on stdout
* and will also...
|
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,...
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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: 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...
|
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...
| |