Use of structure and pointer problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 23
Reputation: rsk8332 is an unknown quantity at this point 
Solved Threads: 0
rsk8332 rsk8332 is offline Offline
Newbie Poster

Use of structure and pointer problem

 
0
  #1
Mar 15th, 2008
Hi,

Suppose I have a character array array1[]={A,B,B,B,A,A}. Now I want to declare a structure with a pointer in it and make each array1 element point to the previous element.For example last array1 element 'A' will point to the previous 'A' which will point to 'B' and so on. Can somebody please help me with the coding?

Any quick help much appreciated.

Thank you
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,753
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Use of structure and pointer problem

 
0
  #2
Mar 15th, 2008
You can't have an array of type char where each char in the array points to another char. The elements in an array either have to be of type char or to type pointer to char, but the elements can't be both at the same time.

Though I have to admit that maybe I'm misreading what you are trying to say?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 23
Reputation: rsk8332 is an unknown quantity at this point 
Solved Threads: 0
rsk8332 rsk8332 is offline Offline
Newbie Poster

Re: Use of structure and pointer problem

 
0
  #3
Mar 15th, 2008
How to declare the array elements of type pointer to char?

Thank you
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,753
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Use of structure and pointer problem

 
1
  #4
Mar 15th, 2008
//declare and initialize an array of type char
char array1[] = {'A', 'B', 'B', 'A'};

Note that array1 is not a string since it is not a null terminated char array.

//declare an array of type pointer to char
char * array2[4];

//assign the address of a char to each pointer in array2
//use the char from array1
array2[0] = &array1[0];
array2[1] = &array1[1];
array2[2] = &array1[2];
array2[3] = &array1[3];

//dereference and display one of the pointers in array2
cout << *array2[2];

output should be: B, if I did it correctly. I didn't compile it first. I'll do that now and post back if needed.
Last edited by Lerner; Mar 15th, 2008 at 9:01 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 23
Reputation: rsk8332 is an unknown quantity at this point 
Solved Threads: 0
rsk8332 rsk8332 is offline Offline
Newbie Poster

Re: Use of structure and pointer problem

 
0
  #5
Mar 15th, 2008
Actually my problem is a bigger one. If I have a character array A,B,B,B,A,A and another character array B,A,A, the output should be as follows:

A B B B A A
- - - B A A

My professor said that I should point each character element of the first array to the previous one. Once I find the path B,A,A in the first string, it should match with the second string B A A which should shift 3 places and align with the first string as shown. Is there a way by which I can point each character element to the previous one via other data structures?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,753
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Use of structure and pointer problem

 
0
  #6
Mar 15th, 2008
I'm sorry, I still do not understand the problem. Did your professor mean start with two sequences:

array1 = abbbaa
array2 = baa

and determine if the sequence in array2 is found in array1. If sequence in array2 found in array1, then create a third array with the elements of array2 shifted to correspond to the elements of array1.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 23
Reputation: rsk8332 is an unknown quantity at this point 
Solved Threads: 0
rsk8332 rsk8332 is offline Offline
Newbie Poster

Re: Use of structure and pointer problem

 
0
  #7
Mar 15th, 2008
I think you understood the problem right. That is my problem. How do I find part of a sequence in one array matches the part of (or whole) sequence in the other array. It can be any two string sequences of A's and B's from the keyboard. I am actually reading them in as character arrays.

Thank you
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,753
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Use of structure and pointer problem

 
0
  #8
Mar 16th, 2008
If the char arrays are strings then you can use substring functions from the cstring or STL string header files. They are standard, but your professor may want you to do this yourself as a learning experience.

I'm afraid that I can't really help you to solve this with pointers to elements of an array as I can't figure out how to do it that way, except to say that the [x] operator is basically the same as p + x where p is a pointer to the first element of the array and x is the number of elements away from p that you are looking at. I find that using pointer syntax when discussing array elements is very cumbersome given the availability of the [] operator so I never use the pointer syntax unless forced to do so by an instruction set.

Heres how I might do it. Take the first char of array2 (or you could start with the last and work backward if you prefer) and look for each instance of that char in array1. If the first char doesn't occur in array1 then array2 can't be a sequence in array1. If you find the first char in array1 then look at each of the next x number of char in array1 and array2, where x is the length of array2 minus 1. If there is a match of all in array1 then the index of the first char match is saved and the sequence in array three starts at that index with spaces preceding it so on display one above the other it looks like array2 is shifted to the appropriate spot.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 23
Reputation: rsk8332 is an unknown quantity at this point 
Solved Threads: 0
rsk8332 rsk8332 is offline Offline
Newbie Poster

Re: Use of structure and pointer problem

 
0
  #9
Mar 16th, 2008
Thanks a lot Lerner. Your idea helped me in coding and giving the right output. However, my two char arrays are not limited to the strings A B B B A A and B A A respectively. They can be any two strings of A's and B's. For example, the first char array may be A B B B A A and the second char array A B B A. Then the output should be as follows:

A B B B A A
A - - B A A

Then how should I modify the above codes so that it works for both pairs of inputs.

Thank you
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,753
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Use of structure and pointer problem

 
0
  #10
Mar 16th, 2008
To find the longest common substring I'd find the shortest string. Then I'd look to see if the entire shorter string was a substring of the longer string. If not, then I'd have to create an array of all possible substrings from the shorter string that could arise by using consecutive char of the substring. For example, if the shorter string were 3 char long then there is one string 3 char long , two substrings 2 char long an 3 substrings 1 char long, though if only A and B are allowed then there's really only 2 substings 1 char long and 2 substrings 2 char long, with duplicates, etc.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC