tags:

views:

211

answers:

5

Hi,

I have the following arrays:

int A[] = {0,1,1,1,1, 1,0,1,0,0, 0,1,1,1,1};
int B[] = {1,1,1,1,1, 1,0,1,0,1, 0,1,0,1,0};
int C[] = {0,1,1,1,0, 1,0,0,0,1, 1,0,0,0,1};
//etc... for all letters of the alphabet 

And a function that prints the letters on a 5x3 LED matrix:

void printLetter(int letter[])

I have a string of letters:

char word[] = "STACKOVERFLOW";

and I want to pass each character of the string to the printLetter function.

I tried:

int n = sizeof(word);

for (int i = 0; i < n-1; i++) {  
    printLetter(word[i]);
}

But I get the following error: invalid conversion from 'char' to 'int*'

What should i be doing?

Thanks!!

+1  A: 

The function is declared as void printLetter(int letter[]), which means it takes a pointer to an array of ints. On the other hand, word is an array of chars, and word[i] is a char, which is not at all the right type. If printLetter() is really just supposed to print a single character, you should change its argument to be a char.

Chuck
Downvoting without comment is incredibly rude and useless. If you have a problem with my answer, say what it is. As far as I know, my answer is exactly right.
Chuck
+1 for a right answer to the wrong question--if you look at the comment left on my answer (which is nearly identical to yours) the OP is -1 because unposted code is failing. Don't you know, you're suppose to read minds.
Alan
A: 
void printLetter(int letter[]) 

should be void printLetter(char letter)

Because: word is a char[] word[i] is a character.

Alan
but the original array that i wan to print is an int array, not a char array: int A[] = {0,1,1,1,1, 1,0,1,0,0, 0,1,1,1,1};If i follow your advice, I get this error: invalid conversion from 'char' to 'char*
majdal
That's because you need to fix printLetter() which is code you haven't posted. My answer is right, given the information you have provided.
Alan
+2  A: 

You cannot so easly transform the 'a' from "stackoverflow" to the A - the array of ints. You can define all the arrays that represent a letter in one single letter and get them by the convertion of your letter to int.

anthares
+4  A: 

Behind the parameter type error there is a deeper issue: you lack the mapping between a char and the corresponding int[].

Redefining printLetter as

void printLetter(char letter)

satisfies the compiler, but doesn't solve your problem per se. Whether inside or outside printLetter, you need to get the corresponding int[] for a given char.

A simple brute-force way to achieve this would be to use a switch, but a better way is to use a second array, i.e. something like this:

void printLetter(char letter) {
  static int* charToMatrix[] = { A, B, C, ... };
  int* matrixToPrint = charToMatrix[letter - 'A'];
  // print the matrix
}

Note that this is an example - I don't have access to a C compiler right now, so I can't guarantee it works straight away, but hopefully it illustrates the point well enough. It also lacks bounds checking, so it accesses memory in strange random places, possibly resulting in a crash, if you attempt to print an "unknown" character.

This solution is supposed to work for the uppercase letters; if you need to print lowercase letters or other characters as well, you might prefer going with an array of 256 elements, where only the elements at indexes corresponding to the "known" matrices are filled, and the rest is set to NULL.

Péter Török
+1  A: 

What you need to do is convert from a character to one of your arrays. So when you have the letter 'A', you'll want to use array A. The easiest way to do this is via lookup table.

int *lookup[256];  // assuming ASCII
memset(lookup, 0, sizeof(lookup));

lookup['A'] = A;
lookup['B'] = B;
...

Then when you have a character, you can get the proper array:

void printletter(char c);
{
    int *data = lookup((unsigned char)c);

    // In case you get a letter that you don't know how to display
    if (data != NULL)
    {
        // display with data
    }
}

Instead of building up your array at runtime, you can also build up your array at compile time although it will be a bit harder as you will need to manually put in NULL pointers.

int *lookup[256] = {
    NULL,   // you need a total of 65 NULL's
    NULL,
    ...
    A,      // so this is at the correct position
    B,
    C,
    ...
};
R Samuel Klatchko