-5

How to search on char that has been randomly generated in an array of structures. If char is found the function must return the info[i].num in array of struct, where info is the array of structures (see code below).

I got error in gcc

 warning: comparison between pointer and integer
 if(info[j].name == s );

how can I use correct comparison ??

#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];  
    int num[5];   
}; 

struct st info[InfoSize] = {{ "some10",6 },{"some50",8},{"some5",4}};

         int r = rand() % (50 + 1 - 10) + 10 ;
         char s = sprintf( arr, "some%d", r );


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

     if(info[j].name == s )
     printf("found  %s  and it's num =%d",info[j].name,info[j].num);

}
VladP
  • 529
  • 3
  • 15
eleen
  • 191
  • 1
  • 4
  • 11

2 Answers2

2

the following proposed code:

  1. cleanly compiles
  2. incorporated the majority of the comments to the question
  3. performs the desired functionality
  4. documents why each header file is included
  5. the code will probably fail to find a matching entry in the strut array because the generation of the arr[] contents could have been any value from 10 to 50 so will never result in a 5 appended to the string: some. And is unlikely to append a 10 or 50.

And now the proposed code

#include <stdio.h>    // printf(), sprintf()
#include <stdlib.h>   // rand(), srand()
#include <string.h>   // strcmp()
#include <time.h>     // time()


#define INFO_SIZE   3
#define MAX_ARR_LEN 20

int main( void )
{
    char arr[ MAX_ARR_LEN +1 ];

    struct st
    {
        char name[20+1];  // +1 to allow for trailing NUL byte
        int num;
    };

    struct st info[ INFO_SIZE ] =
    {
        { "some10",6 },
        { "some50",8 },
        { "some5",4  }
    };

    srand( (unsigned int)time( NULL ) );
    int r = (rand() % 41) + 10 ;
    sprintf( arr, "some%d", r );


    for(int j=0; j<INFO_SIZE; j++ )
    {
         if( strcmp( info[j].name, arr )  == 0 )
         {
             printf("found  %s  and it's num =%d",info[j].name,info[j].num);
         }
    }
}
user3629249
  • 16,402
  • 1
  • 16
  • 17
-1

Your program is missing quite a few things. First order of business is you need to set seed for rand function in order to get pseudo random numbers.

The warning you're getting ( not error, at least on my compiler) tells you you're comparing pointer with integer because an arrays in c are actually plain old pointers.

char s = sprintf( arr, "some%d", r );

According to https://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm sprintf sends output to your arr variable and retruns a value depending on the success of the operation so the following code:

if(info[j].name == s )

should be written as

if(strcmp(info[j].name,arr)==0)

I suggest you try to grasp core concepts of C as it can be of great help in your future programming life even if you never use C again.

Pavisa
  • 102
  • 7