Hi guys,
i m have trouble with reading strrings and storing them in a 2d array and printing them
(1) - for(y=0;y<5;y++)
-
{
-
printf("enter filenames \n");
-
scanf("%s",&fnames[y][0]);
-
}
-
-
for(y=0;y<5;y++)
-
{
-
printf("filename %s\n",&fnames[y][0]);
-
}
-
-
(2) for(y=0;y<5;y++)
-
{
-
printf("enter filenames \n");
-
scanf("%s",fnames[y]);
-
}
-
-
for(y=0;y<5;y++)
-
{
-
printf("filename %s\n",fnames[y]);
-
}
-
for both the programs if the input were to be:
aaa
bbb
ccc
ddd
eee
the output is
aaabbb......eee
bbb.....eee
cc...eee
d...eee
eee
what could be wrong?
i thought fname[y] refers to the base address of yth 1D array in the 2D array.
6 1062
Hi guys,
i m have trouble with reading strrings and storing them in a 2d array and printing them
(1)for(y=0;y<5;y++)
{
printf("enter filenames \n");
scanf("%s",&fnames[y][0]);
}
for(y=0;y<5;y++)
{
printf("filename %s\n",&fnames[y][0]);
}
(2) for(y=0;y<5;y++)
{
printf("enter filenames \n");
scanf("%s",fnames[y]);
}
for(y=0;y<5;y++)
{
printf("filename %s\n",fnames[y]);
}
for both the programs if the input were to be:
aaa
bbb
ccc
ddd
eee
the output is
aaabbb......eee
bbb.....eee
cc...eee
d...eee
eee
what could be wrong?
i thought fname[y] refers to the base address of yth 1D array in the 2D array.
i dnt think anything wrong its workin fine in vc++ compiler
i used gcc in fedora linux..cant figure out the error..
How is your array defined?
hey i got the error..eventhough the program is correct i overlooked some basics..
my array defn was char fnames[5][3]..//i thought this can 3 character strings,but that is not the case..the compiler appends a zero after each input..
so char fnames[5][4] should be correct..
how is your array defined
weaknessforcats ..u were bang on target!!
basics!!
Just a quick check. Does the following sound ok to anyone? It works, but i don't know whether the array gets de-allocated after main() ends? -
#include <iostream>
-
-
int main()
-
{
char* fname[3]; -
-
fname[0] = "aaa";
-
fname[1] = "bbb";
-
fname[2] = "ccc";
-
-
for(int i=0; i<3; i++)
-
std::cout << "\n" << fname[i];
-
-
// The following should be superflous right?
-
/* for(int i=0; i<3; i++)
-
delete [] fname[i];
-
*/
-
-
return 0;}
-
What ytou have defined is an array of 3 char*. This is OK.
Next, each char* is assigned the address of a const string. This is OK.
Next, you display. This is OK.
Next, you delete. This is a crash. You do not delete unless you used the new operator to allocate. When you don't use new, the compiler will take care of deleting your variables.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Federico G. Babelis |
last post by:
Hi All:
I have this line of code, but the syntax check in VB.NET 2003 and also in
VB.NET 2005 Beta 2 shows as unknown:
Dim local4 As Byte
Fixed(local4 = AddressOf dest(offset))
...
|
by: SilverWolf |
last post by:
I need some help with sorting and shuffling array of strings. I can't
seem to get qsort working, and I don't even know how to start to shuffle
the array. Here is what I have for now:
#include...
|
by: Simon Schaap |
last post by:
Hello,
I have encountered a strange problem and I hope you can help me to
understand it. What I want to do is to pass an array of chars to a
function that will split it up (on every location where...
|
by: arkobose |
last post by:
hey everyone!
i have this little problem. consider the following declaration:
char *array = {"wilson", "string of any size", "etc", "input"};
this is a common data structure used to store...
|
by: arkobose |
last post by:
my earlier post titled:
"How to input strings of any lengths into arrays of type: char
*array ?"
seems to have created a confusion. therefore i paraphrase my problem
below.
consider the...
|
by: Michael |
last post by:
Hi,
I am trying to pass a function an array of strings, but I am having trouble
getting the indexing to index the strings rather than the individual
characters of one of the strings.
I have...
|
by: Potiuper |
last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C?
Given: code is written in ANSI C; I know the exact nature of the strings to be...
|
by: Shhnwz.a |
last post by:
Hi,
I am in confusion regarding jargons.
When it is technically correct to say.. String or Character Array.in c.
just give me your perspectives in this issue.
Thanx in Advance.
|
by: Bob Rock |
last post by:
Hello,
I have an array of strings and need to find the matching one with the
fastest possible code. I decided to order the array and then write a binary
search algo.
What I came up with is the...
|
by: Morten71 |
last post by:
I have a strange problem.
I have a local string() var I populate this way:
clmns() As String = {"InvoiceNo", "InvoiceDate"}
When I call:
Array.IndexOf(clmns,"InvoiceDate") I get 0 (zero) as...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |