Home > Software design >  How do I compare a chars array with a char in c ? Why isn't working so?
How do I compare a chars array with a char in c ? Why isn't working so?

Time:10-14

My c code:

#include <ncurses.h>
extern const unsigned int LENGHT=25;
extern unsigned int nv=0;
extern unsigned int charpos [LENGHT];


void CheckStringForChar(char chk, char &str, int N)
{ 
    for (int i = 0; i < N; i  ) {
        if((char)str[i] == chk){
            charpos[nv]=i;
            nv  ;
        }
    }
      
}

int main()
{
 char chk;
 char phrase[LENGHT];
 initscr(); /* Start curses mode */
 printw("Enter check char:"); /* Print Hello World */
 refresh(); /* Print it on to the real screen */
 chk = (char)getch(); /* Wait for user input */

 printf("%c",chk);
 
 addstr("\nEnter phrase to check:");
 refresh();
 getstr(phrase);
 
 printf("\nphrase is: %s",phrase);
 
 CheckStringForChar(chk, *phrase, LENGHT);
 endwin(); /* End curses mode */
 return 0;
}

Gives this error on compile time:

error: invalid types ‘char[int]’ for array subscript

I very recently started learning c/c so I'm aware that this is not the only problem/bad implementation of this code(just trying out). I'm open to any other kind of suggestions.

CodePudding user response:

In the function void CheckStringForChar(char chk, char &str, int N), The char array is in fact not an array. Change the char &str to char *str. Refer to What are the differences between a pointer variable and a reference variable in C ? for the difference between them.

Here is some code that should perform the desired function.

#include <iostream>
#define _SIZE 13

unsigned int length_matchIDXs = 0;
unsigned int matchIDXs[_SIZE];

void compareStr(char match, const char* str, uint32_t size)
{
    length_matchIDXs = 0;

    for(uint32_t n = 0; n < _SIZE;   n)
        matchIDXs[n] = -1;
    
    for(uint32_t i = 0; i < size;   i)
    {
        if(match == str[i])
        {
            matchIDXs[length_matchIDXs] = i;
              length_matchIDXs;
        }
    }

}

int main()
{
    compareStr('o', "Hello, World!", 13);

    std::cout << "Length Of Match Array: " << length_matchIDXs << std::endl
                << "Match Array Data: ";
    
    for(uint32_t n = 0; n < _SIZE;   n)
        std::cout << matchIDXs[n] << ", ";

    return 0;
}

When compiling the above code with const char* str instead being a reference I get the following error from g ;

test.cpp: In function 'void compareStr(char, const char&, uint32_t)':
test.cpp:16:26: error: invalid types 'const char[uint32_t {aka unsigned int}]' for array subscript
         if(match == str[i])
                          ^
test.cpp: In function 'int main()':
test.cpp:28:21: error: invalid conversion from 'const char*' to 'char' [-fpermissive]
     compareStr('o', "Hello, World!", 13);
                     ^~~~~~~~~~~~~~~
test.cpp:7:41: note:   initializing argument 2 of 'void compareStr(char, const char&, uint32_t)'
 void compareStr(char match, const char& str, uint32_t size)

CodePudding user response:

CheckStringForChar needs to compare a char to an array of chars. But instead of passing the whole array, you just pass a pointer to it.

//Declare like this:
void CheckStringForChar(char chk, char* str, int N);
//Call like this: &phrase returns the address of your array
CheckStringForChar(chk, &phrase, LENGHT);
  • Related