Thread: Arrays, pointers and strings

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    29

    Arrays, pointers and strings

    Hi@all

    I am writing a program about pointers and strings but I am stuck on part of it.

    I am trying to read three words from a file. There is only one word in each line therefore I have three lines.

    In this part, the program asks me store words in an array of pointers. The pointers are to be pointers to string objects.

    Now, I am just trying trying to read words and print them. I think there is a logical problem because compiler says everything is ok.

    Can you guys help me to figure out my problem?

    thanks

    My code;

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	string *pointer[3];
    
    	string number;
    
    	ifstream infile;
    
    	infile.open("words.txt");
    
    	for (int i=0; i<3; i++)
    	{
    		infile >> number;
    
    		*pointer[i]=number;
    	}
    
    	for (int m=0; m<3; m++)
    	
    	cout <<*pointer[m]<<' ';
    	
    	cout <<endl;
    
    	return 0;
    }
    Last edited by Apropos; 03-21-2005 at 06:50 PM.
    Not to know, not to learn is shame

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Well the pointer isn't really pointing to anything...

    Code:
    // you would need a string that the pointer is point to
    
    string *pointer;
    string point;
    
    pointer = &point;
    
    //now 'pointer' points to 'poin'

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    29
    thanks @mrafcho001

    I have changed my code to the following code but now I am getting an error..

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	string *pointer[3]; //is this line ok?
    
    	string word;
    
    	pointer=&word;
    
    	ifstream infile;
    
    	infile.open("words.txt");
    
    	for (int i=0; i<3; i++)
    	{
    		infile >> word;
    
    		*pointer[i]=word;
    	}
    
    	for (int m=0; m<3; m++)
    	
    	cout <<*pointer[m]<<' ';
    	
    	cout <<endl;
    
    	return 0;
    }
    I need a tutorial about string class and pointers. Does anyone tell me a link? What is the rule for declaration a pointer to a string (not a c-string) ?

    thanks
    Last edited by Apropos; 03-21-2005 at 07:17 PM.
    Not to know, not to learn is shame

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    string *pointer[3];

    Declares an array of pointers to string. I think you should be using just an array for this task:

    string array[3];

    Then remove the use of '*' everywhere in your program.

  5. #5
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    as Daved said you are declaring an array of pointers you don't want that..
    you want single pointer:
    Code:
    string *pointer;

    actualy when i think about it you dont need the pointer at all just use the string itself..

    EDIT:

    Code:
    //the whole code fixed:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	string *pointer; //is this line ok?
    
    	string word;
    
    	pointer=&word;
    
    	ifstream infile;
    
    	infile.open("words.txt");
    
    	for (int i=0; i<3; i++)
    	{
    		infile >> word;
    
    		*pointer=word;
    	}
    
    	for (int m=0; m<3; m++)
    	
    	cout <<*pointer<<' ';
    	
    	cout <<endl;
    
    	return 0;
    }
    BUT it would really make more sence like this:

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	
    
    	string word[3];
    
    	
    
    	ifstream infile;
    
    	infile.open("words.txt");
    
    	for (int i=0; i<3; i++)
    	{
    		infile >> word[i];
    
    		
    	}
    
    	for (int m=0; m<3; m++)
    	
    	cout <<word[m]<<' ';
    	
    	cout <<endl;
    
    	return 0;
    }
    Last edited by mrafcho001; 03-21-2005 at 07:31 PM.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    29
    @mrafcho001 and Daved

    I see your point but can you explain me the meaning of the following sentence bacause I don't understand what program wants to do from me?

    Quote Originally Posted by Apropos
    In this part, the program asks me store words in an array of pointers. The pointers are to be pointers to string objects.
    Thanks for your help
    Last edited by Apropos; 03-21-2005 at 07:40 PM.
    Not to know, not to learn is shame

  7. #7
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    o you are trying to do like challenge thing?

    you would have an array of pointer pointing to an array of strings i guess..

    so you would have:

    Code:
    string *point1[3]; // or whateve number;
    
    string word[3]; // or whatever number
    
    point1[1] = &word[1];
    point1[2] = &word[2];
    point1[3] = &word[3];
    
    // continue with the code now you can use point1, point2 and point3;
    // they are now point to a string
    OR

    Code:
    string *point1[2];
    string word1, word2, word3;
    
    point1[0] = &word1;
    point1[1] = &word2;
    point1[2] = &word3;
    EDITED^!
    Last edited by mrafcho001; 03-21-2005 at 07:47 PM.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Since you have to use an array of string pointers, then start with your original code. The problem is still that the pointers aren't pointing to anything.

    It makes more sense to use new to allocate space for the three pointers instead of pointing them to word like you did in your second try. I would use new inside the for loop before setting the new value to "number".

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    A brief lesson on pointers:

    All variable names and their corresponding values are stored in memory somewhere. The location in memory where they each reside is denoted with an address. An address is somewhat scary looking:

    006BFDF4

    but you can just think of it as a mailbox number. If you look inside that mailbox number you'll find the variable and its value. A pointer is a variable that stores the address of another variable.

    If you declare a string like so:

    string str = "some text";

    that line stores the variable str with a value of "some text" somewhere in memory. We don't know where it is in memory yet. However, there is an "address of" operator which will get the address of the variable, e.g.

    cout<<&str<<endl;

    You can store that address in a special variable called a pointer:

    string* p;

    That declares a variable named p, and p is a variable of type "pointer to string". Essentially, you read the line in reverse. Now, you can assign the address of any string to p:

    p = &str;

    which reads: "p equals the address of str".

    Finally, you can use a pointer to get the value stored at that address by putting a little star in front of the pointer name:

    cout<<*p<<endl;

    In your code, you declared your array of pointers-to-string correctly, and the array contains three pointers to type string. The names of the pointers are: p[0], p[1], and p[2]. However you have to assign an address of a string to each pointer. So, in your original code, look at this loop:
    Code:
    for (int i=0; i<3; i++)
    {
    	infile >> word;
    
    	*pointer[i]=word;
    }
    1) The first thing you have to do is assign an address to a pointer--dereferencing the pointer before you have assigned anything to it is a mistake. Dereferencing a pointer is a means of getting the value at an address that you have previously assigned to the pointer.

    2) The variable named word has the same address no matter what value it contains, so if you assign the address of word to all the pointers, they will all contain the same address, and the value at that address will be the last value assigned to word. That presents a problem for you, which you have to solve

    Reading input into the same variable is not going to work for you because of the fact that you have to use pointers. If you didn't have to use pointers, you could use one variable to read in the input strings, and then assign each string to a position in an array of strings(v. an array of pointers to string). In that case, the string contained in the variable would be copied to the specified position in the array. However, that doesn't work when using pointers. With pointers, the address of the input variable is copied to the specified position in the array--not the changing value--and the address of the input variable doesn't change.
    Last edited by 7stud; 03-21-2005 at 09:28 PM.

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    29
    Hi 7stud;

    Thanks for the lesson. You are always my angel I think I got you.

    I tried some basic things before editting my prog. as following

    Code:
    
    int *arrayPtr[4];
    
    int *my_array[4]={1,2,3,4};
    
    array[0]=&my_array[0];
    
    cout <<*array[0]<<endl; // everything is ok. I got it
    I changed my code as following and I got junk chars as a output What is wrong with it now

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	string *pointer[3]; //array of pointers
    
    	string number[3];  //array of string
    
    	ifstream infile; //create an object
    
    	infile.open("data.txt"); //open data file to get my string
    
    	for (int i=0; i<3; i++) 
    	{
    		infile >> number[i];  //get first number from the file
    
    		pointer[i]=&number[i]; 
    				
    	}
    
    	for (int m=0; m<3; m++)
    	
    	cout <<*pointer[i]<<' '; //print the values in my array of pointer
    	
    	cout <<endl;
    
    	return 0;
    }
    Last edited by Apropos; 03-21-2005 at 10:31 PM.
    Not to know, not to learn is shame

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I don't see the point in having an array of strings and an array of pointers that point to the strings in the first array.

    Use new to create a new string. Here is an example:

    string* ptr;
    ptr = new string;


    Also, mrafcho001's edited code has an error, the string pointer array in the second code block should be size 3, not 2.

    Finally, in your code you use i+1 as an index. Why? It causes you to access memory outside of the bounds of the array. Just use i.

  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    29

    Thumbs up

    Hi Daved;

    Thanks for your help. I edited my code and used i instead of i+1. I used i+1 because I thought I was getting first string but I made a mistake by doing that. Thanks

    I saw what is wrong with my the last code. In the last loop, I used i instead of m so I was getting junk chars instead of what I am expecting. Now, everything is ok. I will also try to use new. I am glad that I got the point before exam


    edit:

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	string *pointer; //array of pointers
    
    	string number[3];  //array of string
    
    	ifstream infile; //create an object
    
    	infile.open("data.txt"); //open data file to get my string
    
    	for (int i=0; i<3; i++) 
    	{
    	
    		
    		infile >> number[i];  //get first number from the file
    		
    		pointer=new string;
    
    		 pointer=&number[i]; 
    
    	  	cout <<*pointer<<' ';
    	
    				
    	}
    
    	
    		delete pointer;
    
    
    	return 0;
    }
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	string *pointer[3]; //array of pointers
    
    	string number[3];  //array of string
    
    	ifstream infile; //create an object
    
    	infile.open("data.txt"); //open data file to get my string
    
    	for (int i=0; i<3; i++) 
    	{
    		infile >> number[i];  //get first number from the file
    		
    	
    
          pointer[i]=&number[i]; 
    				
    	}
    
    	for (int m=0; m<3; m++)
    	
    	cout <<*pointer[m]<<' '; //print the values in my array of pointer
    	
    	cout <<endl;
    
    	return 0;
    }

    Thanks you guys..
    Last edited by Apropos; 03-21-2005 at 11:02 PM.
    Not to know, not to learn is shame

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Good job!

    This is all wrong though:

    Code:
    int *arrayPtr[4];  //OK
    
    int *my_array[4]={1,2,3,4};  
    //ERROR  You have an array with 4 pointers, and their names are my_array[0], my_array[1],
    //my_array[2], and my_array[3].  You have to assign the address of an int to each of 
    //those pointers.  The values in the intializer list: {1,2,3,4} are not addresses of int's.
    
    array[0]=&my_array[0];  //You haven't declared a variable named 'array'
    
    cout <<*array[0]<<endl; // everything is ok. I got it  
    //How is that possible?  Your code gives me 9 errors.
    Finally, you have to decide if that is the best way to write the code. It really doesn't make sense to have two arrays. If you have studied using the new operator to dynamically allocate memory, I think Daved is suggesting something like this:
    Code:
    infile >> word;
    pointers[i]= new string(word);
    That calls the version of the string constructor that takes another string object as a parameter, and then assigns the address to the pointer.
    Last edited by 7stud; 03-21-2005 at 11:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenating strings (dynamic array using pointers)
    By Tankndozer in forum C Programming
    Replies: 8
    Last Post: 07-01-2004, 07:27 AM
  2. working with strings arrays and pointers
    By Nutka in forum C Programming
    Replies: 4
    Last Post: 10-30-2002, 08:32 PM
  3. hangman need help with strings and pointers
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-27-2002, 09:13 AM
  4. Pointers to pointers to strings
    By Natase in forum C Programming
    Replies: 2
    Last Post: 09-17-2001, 11:30 PM
  5. pointer to pointers to arrays of strings??
    By Binkstone in forum C Programming
    Replies: 8
    Last Post: 09-14-2001, 02:56 AM