Thread: Comparing "Struct Char" with "Char in Char Array"

  1. #1
    Registered User
    Join Date
    Sep 2011

    Comparing "Struct Char" with "Char in Char Array"

    Just making a program to optimize a game I encountered, which is why I didn't put much thought into it.

    For some reason I cannot compare a char in a struct and a char in a char array together (one of them is a pointer?)

    I get the error:
    C:\Users\Unknown\Desktop\5X5 Word Game\main.cpp|71|error: ISO C++ forbids comparison between pointer and integer|


    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    using namespace std;
    struct single
        char letter;
        bool players;
    int main()
        int numWords = 160000;
        int maxLength = 100;
        char **words = new char*[numWords];
        words[0] = new char[numWords*maxLength];
        for ( int i = 1 ; i < numWords ; i++)
            words[i] = words[i-1] + maxLength;
        //Essentially: words[160000][100]
        ifstream ifs("en-US.dic", ios::in);
        for(int a = 0; a<numWords; a++)
            ifs >> words[a];
            //cout << words[a] << endl;
        single Grid[25];
        single tempGrid[25];
        for(int a = 0; a<25; a++)
            Grid[a].letter = 0;
            Grid[a].players = false;
        string tempAllLetters;
        cout << "Enter all letters: ";
        cin >> tempAllLetters;
        for(int b = 0; b<25; b++)
            Grid[b].letter = tempAllLetters[b];
        bool check;
        for(int a = 0; a<numWords; a++) //each cycle checks if each word is possible
            for(int b = 0; b<25; b++)
                tempGrid[b].letter = Grid[b].letter;
            for(int b = 0; b<25; b++)
                check = false;
                for(int c = 0; c<25; c++)
                    if(words[b] == tempGrid[c].letter && words[b] != 0)
    //                    check = true;
    //                    tempGrid[c].letter = 0;
    //                    tempGrid[c].players = true;
                if(check == false)
        return 0;
    My Ctrl+S addiction gets in the way when using Code Blocks...

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    if(words[b] == tempGrid[c].letter && words[b] != 0)
    Well words is a char**, so words[b] is a char*. On the other hand, tempGrid[c].letter is a char. You can't compare a char* directly to a char, and that's what the compiler is complaining about.

    To check whether two char*'s are the same, use strcmp(); to see whether a char is inside a string (word) use strchr(). You could also be using the standard std::string class to make your life easier.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    This is horrible C code with some C++ mixed in. You need to fix this before moving on.
    First off, stop using char arrays. Stop. Forget they exist. For strings, use std::string. Include <string>. Now it's completely natural. No strcmp, no new.
    Don't use dynamic 2D C arrays. Stop. Forget they exist. If you need a 2D array, you can use the slightly convulsed syntax:

    std::vector<std::vector<T>> v(N, std::vector<T>(M));

    To more easily understand what it means, you could think of it in the following way:

    T v[N][M];

    (They are not the same; but it gives you an idea of what it does.)
    In your case, however, you do not need a 2D array. You need a 1D array of strings, so:

    std::vector<std::string> v(size);

    Better yet, since the number of words are fixed, you could do

    std::array<std::string, size> words;

    If you really need a dynamic array (ie, an array whose size changes over time, or whose size is determined at runtime), you could simply do

    std::vector<std::string> words;
    words.push_back("my string");

    The container will automatically expand as it needs more room to store your strings.
    They also do not require you to call delete as you need in your current code (which currently leaks memory - which is very bad!).

    I don't know what the rest of your code does. Though, I presume it searches through a file for matches on a word or some string of letters.
    You are much better of using stream iterators ( or just reading line-by-line (std::getline). You save tons of memory and much complexity.

    std::vector requires you to include <vector>.
    std::array requires you to include <array>.
    Last edited by Elysia; 12-11-2012 at 03:09 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "invalid conversion from 'char' to 'const char' "
    By gurharman in forum C++ Programming
    Replies: 8
    Last Post: 11-24-2010, 10:43 AM
  2. Char Help! "Packing " bits to a signle unsigned char
    By xxrexdartxx in forum C Programming
    Replies: 7
    Last Post: 10-11-2009, 04:45 AM
  3. Replies: 9
    Last Post: 07-07-2006, 11:03 AM
  4. help "wrapping" data types in char array
    By hampycalc in forum C Programming
    Replies: 5
    Last Post: 03-09-2006, 02:36 PM
  5. Converting a "Char" string to an Integer Array
    By Jazz in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2001, 01:50 PM