473,321 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

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

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
  1.  
  2. struct name{
  3. char firstName[10];
  4. };
  5.  
  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];
  12.  
  13.     baseDataSet=dataSet;
  14.     baseSearchAddress = searchPtr;
  15.  
  16.     for(int i=0;i<3;i++){
  17.     cout<<"Please enter 3 names\n";
  18.     cin.getline(dataSet->firstName,10);
  19.     dataSet++;
  20.     }
  21.  
  22.     dataSet=baseDataSet;
  23.  
  24.     cout<<"Please enter the search term:\n";
  25.     cin.getline(searchPtr->firstName,10);
  26.  
  27.     searchPtr=baseSearchAddress;
  28.     dataSet=baseDataSet;
  29.  
  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
4 2765
DeMan
1,806 1GB
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. }
  4.  
Mar 22 '07 #2
DeMan
1,806 1GB
Incidently, I'm not sure why you are incrementing your search pointer....?
Mar 22 '07 #3
vaiism
9
DeMan,

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
DeMan
1,806 1GB
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);
  2.  
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;
  11.  
  12.       }
  13.       //searchPtr++; 
  14.    }
  15.  
I think we still need refining, but this will give you better results
Mar 22 '07 #5

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

Similar topics

11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
1
by: mrhicks | last post by:
Hello all, I need some advice/help on a particular problem I am having. I have a basic struct called "indv_rpt_rply" that holds information for a particular device in our system which I will...
7
by: techno | last post by:
Dear all, Our bitmap has some x00 values ( '\0' ) and i am storing it in char* array. the problem is that the '\0' is treated as eos character in c and it is truncating it so the characters...
0
by: harsha1305 | last post by:
Hi all, I need to create a pointer to array of structure. Definition of structure: typedef struct { char b_name; unsigned long int sig; unsigned long int count; volatile unsigned char...
13
by: Kantha | last post by:
Hi all, I have declared an Union as follows typedef union { struct interrupt_bits { unsigned char c_int_hs_fs_status : 1, c_setup_intflag : 1,
9
by: sean.scanlon | last post by:
can someone help understand how i can could access a struct field dymanically like: foo->fields ? when i try to compile this i get the following error: 'struct pwd' has no member named 'fields'...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
4
by: ctx2002 | last post by:
hi guys: I am reading Sqlite code at moment, my c language skill not good enough , hope some one here can help me. In function listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg);...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.