473,395 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

array of strings

Hi guys,

i m have trouble with reading strrings and storing them in a 2d array and printing them

(1)
Expand|Select|Wrap|Line Numbers
  1. for(y=0;y<5;y++)
  2.     {
  3.      printf("enter filenames \n");
  4.      scanf("%s",&fnames[y][0]);
  5.     }
  6.  
  7.   for(y=0;y<5;y++)
  8.     {
  9.      printf("filename %s\n",&fnames[y][0]);
  10.     }
  11.  
  12. (2) for(y=0;y<5;y++)
  13.     {
  14.      printf("enter filenames \n");
  15.      scanf("%s",fnames[y]);
  16.     }
  17.  
  18.   for(y=0;y<5;y++)
  19.     {
  20.      printf("filename %s\n",fnames[y]);
  21.     }
  22.  
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.
Mar 24 '08 #1
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
Mar 24 '08 #2
i used gcc in fedora linux..cant figure out the error..
Mar 24 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
How is your array defined?
Mar 24 '08 #4
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!!
Mar 24 '08 #5
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?
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. int main()
  4. {
        char* fname[3];
  5.  
  6.     fname[0] = "aaa";
  7.     fname[1] = "bbb";
  8.     fname[2] = "ccc";
  9.  
  10.     for(int i=0; i<3; i++)
  11.         std::cout << "\n" << fname[i];
  12.  
  13.    // The following should be superflous right? 
  14.    /* for(int i=0; i<3; i++)
  15.          delete [] fname[i];
  16.    */
  17.  
  18.   return 0;}
  19.  
Mar 24 '08 #6
weaknessforcats
9,208 Expert Mod 8TB
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.
Mar 25 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
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)) ...
3
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...
4
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...
7
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...
12
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...
24
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...
2
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...
14
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.
11
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...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
jinu1996
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...
0
tracyyun
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...
0
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,...

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.