LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-20-2008, 10:56 AM   #1
traene
Member
 
Registered: Jan 2005
Distribution: Archlinux, Debian, Centos
Posts: 222

Rep: Reputation: 35
gcc Warning: Array index has type 'char'


While trying to compile a programme, i get the following warning:

tokenizer.cpp:121: warning: array subscript has type 'char'

The array is a table mapping special meanings to the ASCII character sets. For instance i want to extract quotes, whitespace and such:

Code:
#include <stdio.h>
#include <string.h>

const char tokTable[128];

void parseInput(const char* input) {
    int posCount=0;
    while (*input) {
        if (tokTable[*input]==1){
            printf("found a space at pos: %d\n",posCount);
        }
        ++input; ++posCount;
    }
}

int main(int argc, const char** argv) {
    char* ptok=(char*)tokTable;
    bzero(ptok,sizeof(tokTable));
    ptok[32]=1; // space
    //tokTable[]; and so forth
    parseInput("Hey you!");
    return 0;
}
How can i fix the warning at: 'tokTable[*input]'

I am using gcc 4.3.0
 
Old 04-20-2008, 01:05 PM   #2
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Cast it to int. But I can't see why it's giving a warning for a lossless conversion.
 
Old 04-20-2008, 01:33 PM   #3
traene
Member
 
Registered: Jan 2005
Distribution: Archlinux, Debian, Centos
Posts: 222

Original Poster
Rep: Reputation: 35
That did the trick.

Thank you.
 
Old 04-20-2008, 02:09 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by Dan04 View Post
But I can't see why it's giving a warning for a lossless conversion.
The warning is enabled by “-Wchar-subscripts” or its superset “-Wall” as a command-line option. It is only a warning (not an error), and your usage is valid C++. The reason there is a warning for this is that the type char may be signed, in which case a high-bit value might be a negative index in your subscript (which may or may not be what you want, but is a common source of bugs).
 
Old 04-20-2008, 08:43 PM   #5
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
That makes sense. I've been bitten by signed chars a few times.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc 4.0.0 warning: dereferencing type-punned pointer will break strict-aliasing rules stephenwalter Programming 4 06-10-2009 04:06 AM
error: array type has incomplete element type nasim751 Linux - Software 1 04-14-2008 09:02 PM
Warning ___ specifies undefined mime type/service type mbvpixies78 Linux - Software 0 12-29-2007 10:25 PM
const array parameter warning in GCC JoeyAdams Programming 2 09-06-2007 03:15 PM
arrays of elements with [gcc4]array type has incomplete element type lmmix Linux - Software 0 02-26-2005 08:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration