| | |
Use of structure and pointer problem
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 23
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jul 2005
Posts: 1,753
Reputation:
Solved Threads: 283
//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.
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.
•
•
Join Date: Feb 2008
Posts: 23
Reputation:
Solved Threads: 0
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?
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?
•
•
Join Date: Jul 2005
Posts: 1,753
Reputation:
Solved Threads: 283
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.
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.
•
•
Join Date: Jul 2005
Posts: 1,753
Reputation:
Solved Threads: 283
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.
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.
•
•
Join Date: Feb 2008
Posts: 23
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jul 2005
Posts: 1,753
Reputation:
Solved Threads: 283
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.
![]() |
Similar Threads
- Accessing To A Struct With A Given Pointer (C)
- i really hate pointers (C++)
- Having trouble trying to use a global structure (C)
- structures containing a pointer to an array (C++)
- How to delete a data structure in Builder 6.0? (C++)
Other Threads in the C++ Forum
- Previous Thread: Composition problem "'XXX' is not a base or member"
- Next Thread: C++ Functions
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






