Click here to Skip to main content
14,694,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm asking how can I search on char if it is found in array of struct or not .

What I have tried:

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define InfoSize  3 

int main(int argc, char *argv[]) 
{  
char arr[20];
struct st
{    
    char name[20];  
      
}; 

struct st info[InfoSize] = {{ "sl" },{"sos"},{"ss"}};

         
         char s = 'ss';


  for(int j=0;j<3;j++){

     if(info[j].name == s )
     printf("found  %s  ",info[j].name);

}
Posted
Updated 25-Dec-17 22:49pm
v3
Comments
Richard MacCutchan 26-Dec-17 3:03am
   
This will not even compile. You have declared s as a character, and then tried to put two characters in the constant. And you then try to compare an array with a character. You need to learn the basics of C character handling.

it very easy pointer arithmetics:
struct st *pointer = &info[0];//getting pointer to address of first struct
char c = 'a';//search char
int pos = -1;//external scope of var
for( pos = 0; pos < 50/*size of name*/; pos++ ) {
 if( pointer->name[pos] == c )//check 
   break;
} 
Consider learning C with some tutorials.
   
char[2] s = "ss";


for(int j=0;j<3;j++){

   if(strcmp(info[j].name, s) == 0 )
   printf("found  %s  ",info[j].name);
   
Comments
jeron1 26-Dec-17 13:44pm
   
's' should have a size of 3, to account for the null terminator.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900