Visualizzazione dei risultati da 1 a 5 su 5

Discussione: [C] Warning GCC

  1. #1

    [C] Warning GCC

    ciao a tutti

    spero perdonerete la mancanza di indentazione nel codice ma il mio browser non mi permette di editarlo diversamente

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

    #define MAX_GET 1024
    #define MAX_KEYS 100
    #define MAX_VALUES 100

    static char *keys[MAX_KEYS], *values[MAX_VALUES];

    int isMethod(char *m);
    void getPostParams(void);
    void getGetParams(void);

    int main(int argc, char *argv[]){
    printf("%s\n\n", "Content-Type: text/html");
    if(isMethod("GET")) getGetParams();
    return(0);
    }

    int isMethod(char *m){
    if(strcmp(getenv("REQUEST_METHOD"), m) != 0) return 0;
    return 1;
    }

    void getGetParams(){
    int i, j;
    char *query_string, *tmp, *params[100];
    query_string = getenv("QUERY_STRING");
    tmp = query_string;
    for(i = 0; i < MAX_GET; ++i){
    if(tmp == NULL) break;
    tmp = strtok(tmp, "&");
    params[i] = tmp;
    tmp = strtok(NULL, "&");
    }
    for(j = 0; j <= i; ++j){
    tmp = params[j];
    if(tmp == NULL) break;
    tmp = strtok(tmp, "=");
    keys[j] = tmp;
    tmp = strtok(NULL, "=");
    values[j] = tmp;
    }
    return;
    }

    quando compilo mi vengono restituiti i seguenti warning

    cgilib.c: In function `isMethod':
    cgilib.c:21: warning: passing arg 1 of `strcmp' makes pointer from integer withut a cast
    cgilib.c: In function `getGetParams':
    cgilib.c:28: warning: assignment makes pointer from integer without a cast

    ma il programma funziona senza problemi. Non è molto che programmo e vorrei capire come risolverli


    grazie
    ciao

  2. #2
    Utente di HTML.it L'avatar di anx721
    Registrato dal
    Apr 2003
    Messaggi
    2,352
    Il warning dice che usi un intero come puntatore senza fare il cast esplicitamente, il che potrebbe essere sintomo di un bug.

    Cosa restituisce getenv("REQUEST_METHOD")?

    Dal messagio sembra che restituisca un intero, e tu lo usi cone un puntatore a char passandolo come parametro della funzione strcmp. Per evitare il warning dovresti castarlo a (char *) esplicitamente, ma è questo quello che vuoi? sei consapevole di cio che fai?

  3. #3
    in effetti pensavo che getenv dovesse restituire un puntatore a char ed è proprio questo quello che voglio, però evidentemente la funzione restituisce un intero, quindi adesso provo a fare il cast

    ti ringrazio

  4. #4
    Utente di HTML.it L'avatar di anx721
    Registrato dal
    Apr 2003
    Messaggi
    2,352
    Secondo me dovresti studiare un po piu attentamente quello che stai facendo comunque, non dovrebbe essere il compilatore a dirti di fare il cast, e se non l'avevi gia pensato significa che qlcosa non va nel codice, poi castare un intero a puntatore a char non ha molto senso.


  5. #5
    Originariamente inviato da matteolandi
    in effetti pensavo che getenv dovesse restituire un puntatore a char ed è proprio questo quello che voglio, però evidentemente la funzione restituisce un intero
    Penso che il problema sia altrove, perche`...

    codice:
    GETENV(3)                  Linux Programmer's Manual                 GETENV(3)
     
    NAME
           getenv - get an environment variable
     
    SYNOPSIS
           #include <stdlib.h>
     
           char *getenv(const char *name);
     
    DESCRIPTION
           The  getenv()  function searches the environment list for a string that
           matches the string pointed to by name.  The strings  are  of  the  form
           name = value.
     
    RETURN VALUE
           The  getenv()  function  returns a pointer to the value in the environ-
           ment, or NULL if there is no match
    "Qualsiasi esperto ha paura di combattere usando la katana vera. Anch'io. Ma non ignoro la mia paura, riesco ad accettarla, e a metterla da parte accanto a me".

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.