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

## All 14 Replies

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?

How to declare the array elements of type pointer to char?

Thank you

//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.

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?

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.

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

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.

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

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.

Thanks for your suggestion, Lerner. Actually my character arrays can be of maximum 40 characters. Then it really becomes difficult to find all substrings of the shorter array if it contains 30 characters for example. Is there some simpler way?

Thank you

Not that I can think of if the goal is to find the longest common substring of two original strings. But there's lots of bright people here, maybe someone else will have an idea.

Can somebody suggest the best way to concatenate char array elements?

Thank you

I want to thank you, Lerner. I have almost finished a majority of my program codes based on your ideas and suggestions. I simply have to give a finishing touch to the program. Since noone else responded, I followed up your method and the main thing is that it works!!

Thank you again!!

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

i think your prof wanted you to use link list. in a link list you can keep the pointer to the next element.

struct node
{
char a;
node* p;
}

here you can assign the valus of 'a' from the array element and then keep a pointer to the next element.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.