0

h, i am student and study C programming, by lab-windows CVI.. in GUI i have create text-box and list-box... i read from a file to text-box and the user, searches after a word... when it is matched, it should appears the word in the list-box... i have used a character word, (first as pointer or an array) but used a keyword to put the word in the list-box, but i get error that: it is pointed to char and expect char. i changed it to simple character, but it complains that it is small, even i have allocated... i hope that you can solve my problem.. appreciate... `int i=0, textLength; char str[80], word1[25], word2;

static FILE *ifp;

switch (event)
{
    case EVENT_COMMIT:

        ifp = fopen ("text.txt", "r");
        while((fgets(str,80,ifp))!=NULL) {
            SetCtrlVal (panelHandle, PANEL_TEXTBOX, str);
            SetCtrlVal (panelHandle, PANEL_TEXTBOX, "\n");
            ++i;
    }
        rewind(ifp);
        GetCtrlAttribute (panelHandle, PANEL_STRING, ATTR_STRING_TEXT_LENGTH, &textLength);
        word2 = (char) malloc (sizeof(char) * (textLength + 1));  
        GetCtrlVal (panelHandle, PANEL_STRING, &word2); //argument too small using usual char

        while((fscanf(ifp,"%s",word1))!=EOF){
            if(strcmp(word1,&word2)==0){
                SetCtrlVal(panelHandle, PANEL_LISTBOX, word2); //get error, it is pointed to char and expect char (when *word2 or word[25]) ?!!
                if ((panel2 = LoadPanel (0, "ex1.1.1.uir", PANEL_2)) < 0)
                    return -1;
                DisplayPanel (panel2);
                SetCtrlVal(panel2,PANEL_2_TEXTMSG,"match found");
            }

        }
        break;`    
  • Shouldn't `char word2` be `char *word2`, followed by `word2 = malloc(textLength + 1);` and by `if(strcmp(word1,word2)==0)` ? Please read [Do I cast the result of malloc?](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) By casting you hid a warning from the compiler. – Weather Vane Nov 09 '17 at 09:48
  • ...and `GetCtrlVal (panelHandle, PANEL_STRING, word2);` – Weather Vane Nov 09 '17 at 09:55

1 Answers1

1

You casted the return type of malloc to char instead to char* (pointer to char). Type word2 = (char*) malloc (sizeof(char) * (textLength + 1)); instead or avoid casting entirely.

Daniel
  • 144
  • 1
  • 9