-1

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.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 4
    Your code is not comparing a "char array" with a `char` at all. `char &str` declares `str` as a reference to a `char` (i.e. an aliased name for a single `char`) not an array, and the compiler diagnosing an error on the expression `str[i]`. Your attempted workaround with a cast (i.e. `(char)str[i]`) does not fix. To fix, do three things (1) change `char &str` in the argument list of `CheckStringForChar()` to `char *str` or (equivalently) to `char str[]` (2) remove the `(char)` type conversion from `(char)str[i]` (3) in `main()`, pass `phrase` as the second argument instead of `*phrase`. – Peter Oct 14 '21 at 00:10
  • And you need to focus on reading your textbook (or other introductory material) on C++, BEFORE trying to write code and having to *guess* what works. Your guess - however you came to it - was badly wrong in this case. – Peter Oct 14 '21 at 00:13
  • General rule of thumb: Using a cast to fix a compiler error can often silence the compiler error and leave you with a harder-to-find runtime error. Know exactly what you are fixing and how before deploying a cast. – user4581301 Oct 14 '21 at 00:14
  • Are you trying to compare a **single character** with an array of characters? Or are you trying to find a *single character* among many in an array? – Thomas Matthews Oct 14 '21 at 00:15
  • Please don't tag spam. Your question has nothing to do with either C++ or *performance*. Tags have relevance and meaning here - please use only tags that actually are applicable to your question. Tag abuse is a very good way to quickly collect downvotes here. Thanks. – Ken White Oct 14 '21 at 00:16
  • 3
    You have both C and C++ language tags listed. Are you mixing them? They are distinct languages, for example, C++ has `std::string` for text and C doesn't. In C you need to use `strcmp` to compare C-style strings. Please update your language tags with the language you are programming in. – Thomas Matthews Oct 14 '21 at 00:17
  • `learning c/c++` Pick one. Ignore the other. – wildplasser Oct 14 '21 at 00:19
  • This code is not C. C has no "reference" type-qualifier. Edit the code, or edit the tags again. – mlp Oct 14 '21 at 02:21

2 Answers2

0

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)

RRKS101_1 TF2
  • 89
  • 1
  • 1
  • 7
0

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);
ventsyv
  • 3,316
  • 3
  • 27
  • 49
  • Since `phrase` is a `char[]` array, you don't need `&` to pass it, let it *decay* into a `char*` for you, eg: `CheckStringForChar(chk, phrase, LENGHT);` – Remy Lebeau Oct 14 '21 at 01:38