By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,849 Members | 2,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,849 IT Pros & Developers. It's quick & easy.

Using a Pointer to access individual elements of a char array in a struct

P: 9
I am trying to write a string compare function using pointers and dynamic arrays, and am having trouble comparing the individual elements of the array contained with the struct. The code below works, but I was wondering if there was a way to use pointers instead of the index [j] in the last for loops to compare the individual components of the char array contained in the struct? (I also have code to determine the size of the char array, dependent on using the indices, as well as the proper delete []'s at the end of the program)

Thank you.
Expand|Select|Wrap|Line Numbers
  2. struct name{
  3. char firstName[10];
  4. };
  6. Body of code:
  7. {
  8.     name *baseDataSet, *baseSearchAddress;
  9.     name *searchPtr=new name[1];
  10.     name *dataSet=new name[3];
  11.     name *namePtr=new name[3];
  13.     baseDataSet=dataSet;
  14.     baseSearchAddress = searchPtr;
  16.     for(int i=0;i<3;i++){
  17.     cout<<"Please enter 3 names\n";
  18.     cin.getline(dataSet->firstName,10);
  19.     dataSet++;
  20.     }
  22.     dataSet=baseDataSet;
  24.     cout<<"Please enter the search term:\n";
  25.     cin.getline(searchPtr->firstName,10);
  27.     searchPtr=baseSearchAddress;
  28.     dataSet=baseDataSet;
  30.     for(int i=0;i<3;i++){
  31.             for(int j=0;j<5;j++){
  32.                     if(searchPtr->firstName[j]==(dataSet->firstName[j]){
  33.                                  cout<<"Match!\n";
  34.                     }
  35.                                  searchPtr++; 
  36.             }
  37.             searchPtr=baseSearchAddress;
  38.             dataSet++;  
  39.     }
  40. }
Mar 21 '07 #1
Share this Question
Share on Google+
4 Replies

P: 1,806
The beginning of an array is a pointer so you could try something like

Expand|Select|Wrap|Line Numbers
  1. if(*(searchPtr->firstName+j)==*(dataSet->firstName+j)){
  2.     cout<<"Match!\n";
  3. }
Mar 22 '07 #2

P: 1,806
Incidently, I'm not sure why you are incrementing your search pointer....?
Mar 22 '07 #3

P: 9

Thanks for the help, I was trying to use ++ to increment the pointer to the array, and it was producing erroneous results.

The goal was to compare each character in the search array (search term) with the array of structs as entered previously in the program. When I compared the values without looking at each character (say entering Bob into both the array of structs and the search term) it wouldn't recognize that there was a match even though an output to the screen would show the same thing, and comparing each character in the array seemed to be the only way to get it to work.
Mar 22 '07 #4

P: 1,806
I assume from your reply that you have solved the problem....but I will post this though anyways,,,,
When I ran the program, it recognised matches, but it was finding them by comparing the same string (that is, not comparing to the searchterm)

I think your input function will always only store the LAST name....try using
Expand|Select|Wrap|Line Numbers
  1. cin.getkline(dataSet[i].firstName,10);
Secondly set up a flag so that it is changed if any character is different.....
Expand|Select|Wrap|Line Numbers
  1. int match = 1;
  2.  for(int i=0;i<3;i++){
  3.     match = 1;
  4.     for(int j=0;j<5;j++){
  5.       if(*(searchPtr->firstName+j)!=*(dataSet[i].firstName+j)){
  6.         match=0;  
  7.       }
  8.       if(match == 1)
  9.       {
  10.           cout << "Match " << searchPtr->firstName << " with " << dataSet[i].firstName;
  12.       }
  13.       //searchPtr++; 
  14.    }
I think we still need refining, but this will give you better results
Mar 22 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.