It is currently Fri May 26, 2017 11:27 pm




 Page 1 of 1 [ 7 posts ] 
Author Message
 Post subject: array of strings
PostPosted: Thu Aug 28, 2008 3:26 pm 

Joined: Fri Jan 04, 2008 2:11 pm
Posts: 72
i declared an array of strings say ...

string example[10];

and then tried reading values into them from a text file...whose values are as below:

hello
hi
there
how
are
you

but the strings don't get stored as expected:
example[0]="hello"
example[1]="hi"
example[2]="there"
.
.
..but as:

example[0]="h"
example[1]="e"
example[2]="l"
example[3]="l"
example[4]="o"

i tried typecasting
fin>>(string)example[i];
but then they are just empty strings....how do i rectify this??


Offline
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2008 4:52 am 

Joined: Sat Apr 19, 2008 6:26 am
Posts: 62
You declared 10 char in string. You must declared 10 string. This is very simple code how to create.

#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

void main()
{
    char *tab[10];

    tab[0] = "one";
    tab[1] = "two";
    tab[2] = "three";
    tab[3] = "four";
    tab[4] = "five";
    tab[5] = "six";
    tab[6] = "seven";
    tab[7] = "eight";
    tab[8] = "nine";
    tab[9] = "ten";
   
    for(int i = 0 ; i< 10; i++){
        cout << "Tab" << i << " == "<< tab[i] << endl;
    }
    getchar();
    return;
}


Offline
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2008 9:31 am 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
chester30, I'm surprised your code example doesn't crash my computer! you've declaired 10 char* but haven't allocated memory to them before you started sticking things into them. I guess the compiler knows to allocate enough memory to fit what ever you want to go in each char*. Interesting!

Here is what I did with std::string

std::string strExample[10];

    strExample[0] = "one";
    strExample[1] = "two";
    strExample[2] = "three";
    strExample[3] = "four";
    strExample[4] = "five";
    strExample[5] = "six";
    strExample[6] = "seven";
    strExample[7] = "eight";
    strExample[8] = "nine";
    strExample[9] = "ten";
   
    for(int i = 0 ; i< 10; i++){
        printf("%s\n", strExample[i].c_str());
    }


It appears to work fine.


Offline
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2008 11:54 am 

Joined: Fri Jan 04, 2008 2:11 pm
Posts: 72
ur code is same as mine..but i'm reading from a file and not assigning values manually
data in file:
----------------------------------------
6
hello
hi
there
how
are
you

---------------------------------------
my code:

fin("read.in");
fin>>no;
for(int i=0;i<no;i++)
{
    fin>>example[i];
}


for some reason it reads individual characters not strings from the file


Offline
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2008 2:25 pm 

Joined: Sat Apr 19, 2008 6:26 am
Posts: 62
You must create a file test.txt

c++ code
 
#include <iostream>                              
#include <string>                              
#include <fstream>                              
using namespace std;                        

void main(){                                          
   ifstream file;                              
                            
   string readLine = "";
                                    
   file.open("test.txt");                        
                                                                                                                           
   if(file.fail())   {                                       
      cout << "Error not find test.txt!\n";                                    
      return;                                 
   }   
   
    while(getline(file, readLine))            
      cout << readLine << endl;                                    
                                                                              
   file.close();                              

   getchar();
   return;
}


Offline
 Profile  
 
 Post subject:
PostPosted: Sun Jun 07, 2009 12:31 pm 

Joined: Sun Jun 07, 2009 12:00 pm
Posts: 1
cool


Offline
 Profile  
 
 Post subject:
PostPosted: Tue Jun 16, 2009 10:24 am 

Joined: Tue May 01, 2007 2:55 pm
Posts: 96
Location: Behind you
Marek wrote:
I guess the compiler knows to allocate enough memory to fit what ever you want to go in each char*.


I found this rather hard to believe, so I looked into what was really happening.
This turns out to be an artifact of c using char[] to represent strings, while allowing string constants (literals in c++). I'll simplify the example to the following:
void main()
{
    char *tab;

    tab = "one";
   
    cout << "Tab " << tab;
}

Here the same thing occurs, and as we removed unnecessary complications, it is easier to see what is happening.
Now we have a statement that does: "char *=char[]"
Automatically the array of characters is converted into a pointer. So basically what we store in tab is the address of the constant "one".

The question that follows is: Do constants have memory addresses? The answer is that yes they do have memory addresses. If they did not have memory addresses, they could not be treated in the same way non-literals can. So literals are stored in memory too, and as such they do have memory addresses.

What escapes me is why there is no const-correctness forced here. Try the following code:
void main()
{
    char *tab;

    tab = "one";
   
    tab[0] = 't';
    tab[1] = 'w';
    tab[2] = 'o';
}

I don't know how this fares on other compilers, but on gcc this crashes at runtime and indeed, if you look it up you will find that "trying to modify a literal results in undefined behavior. I wonder why this is defined as such without the string literal being defined as const char[]. However, we are talking about design choices made for c rather than those made for c++.


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 7 posts ] 


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

cron