Pagina 4 di 4 primaprima ... 2 3 4
Visualizzazione dei risultati da 31 a 38 su 38

Discussione: [C] Puntatori a char

  1. #31
    solo se viene passato come parametro di una funzione.
    esempio
    codice:
    funzione(char array[],int dim){
    int i;
    for(i=0;i<(dim-1)/2;i++)array[i]=array[dim-1];
    
    }
    attenzione che facendo in questo modo(cioe' dichiarando un parametro come array non definito), si ottiene si' un grado in piu' di astrazione, ma se mal gestito si rischia il buffer overflow

    comunque nel main l'array andra' definito o comunque creato con una malloc.

  2. #32
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,328
    Originariamente inviato da teju
    Lo credevo anch'io che fosse completamente sbagliato, ma ho trovato più e più fonti che mi mostrano array senza valore dentro le parentesi, libro di programmazione C compreso...
    Dunque inizio a credere sia fattibile.... no?
    Per curiosità, che libro è?

    Spero che su quel libro sia indicato esplicitamente l'utilizzo di un particolare compilatore, perchè non ho trovato nessun compilatore che accetti una tale dichiarazione (eccezione fatta, appunto, per gli argomenti delle funzioni, come fatto giustamente notare da kentaromiura).


    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

  3. #33
    Utente di HTML.it L'avatar di anx721
    Registrato dal
    Apr 2003
    Messaggi
    2,352
    un altro caso in cui il compilatore, almeno gcc, accetta un array senza dimensione è nella specifica di una struct, ad esempio:

    typedef struct{

    int a[];

    } prova;

    anche se non sono riuscito a capire quale sia l'effetto.

    Sun Certified Java Programmer

    EUCIP Core Level Certified

    European Certification of Informatics Professionals

  4. #34
    Originariamente inviato da anx721
    un altro caso in cui il compilatore, almeno gcc, accetta un array senza dimensione è nella specifica di una struct, ad esempio:

    typedef struct{

    int a[];

    } prova;

    anche se non sono riuscito a capire quale sia l'effetto.
    http://home.tiscalinet.ch/t_wolf/tw/c/c9x_changes.html
    punto 28 e punto 33

    [28]
    Structs: the last member may have an incomplete array type. (This is a way to codify the well-known "struct hack" that was widely used and in practice worked on nearly every compiler.) The idea is illustrated by the following piece of code:
    codice:
    struct s {int n; double d[];};
    struct s *p1, *p2;
    size_t   sz;
    
    sz = sizeof (struct s);  // sz == offsetof (struct s, d)
    
    p1 = malloc (sizeof (struct s) + 8 * sizeof (double));
    p2 = malloc (sizeof (struct s) + 5 * sizeof (double));
    
    /* p1 behaves now as if it had been declared as
    
       struct {int n; double d[8];} *p1;
    
       p2 behaves now as if it had been declared as
    
       struct {int n; double d[5];} *p2;
    */
    Note that the specification as given in the Committee Draft implies that there be no padding before the variable last member, or, if there is, that it be included in sizeof (struct s).

    si puoi mettere come ultimo elemento un array,
    ma poi devi allocare la struttura col malloc usando il trucchetto sopraelencato
    sizeof(struct s)+N* sizeof(tipo)

  5. #35
    Utente di HTML.it L'avatar di anx721
    Registrato dal
    Apr 2003
    Messaggi
    2,352
    Originariamente inviato da kentaromiura
    http://home.tiscalinet.ch/t_wolf/tw/c/c9x_changes.html
    punto 28 e punto 33

    [28]
    Structs: the last member may have an incomplete array type. (This is a way to codify the well-known "struct hack" that was widely used and in practice worked on nearly every compiler.) The idea is illustrated by the following piece of code:
    codice:
    struct s {int n; double d[];};
    struct s *p1, *p2;
    size_t   sz;
    
    sz = sizeof (struct s);  // sz == offsetof (struct s, d)
    
    p1 = malloc (sizeof (struct s) + 8 * sizeof (double));
    p2 = malloc (sizeof (struct s) + 5 * sizeof (double));
    
    /* p1 behaves now as if it had been declared as
    
       struct {int n; double d[8];} *p1;
    
       p2 behaves now as if it had been declared as
    
       struct {int n; double d[5];} *p2;
    */
    Note that the specification as given in the Committee Draft implies that there be no padding before the variable last member, or, if there is, that it be included in sizeof (struct s).

    si puoi mettere come ultimo elemento un array,
    ma poi devi allocare la struttura col malloc usando il trucchetto sopraelencato
    sizeof(struct s)+N* sizeof(tipo)
    Grazie ;

    certo che gli inventori del C le hanno pensate tutte per favorire errori di memoria...

    Sun Certified Java Programmer

    EUCIP Core Level Certified

    European Certification of Informatics Professionals

  6. #36
    Originariamente inviato da Brolli
    codice:
    char* RemoveDot(char *path)
    {
    	char *ptr;
    	char *remain;
    
    	if(!(ptr = strstr(path, "./"))) return path;
    	if(ptr == path) path = path + 2;
    	remain = ptr + 2;
    	path = strncpy(path, path, strlen(path) - strlen(ptr));
    	path = strcat(path, remain);
    	return RemoveDot(path);
    }
    non so più come fare... dà segmentation fault...

    come lo sistemo? :master:
    help
    P.

  7. #37
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,328
    Originariamente inviato da Brolli
    codice:
    char* RemoveDot(char *path)
    {
    	char *ptr;
    	char *remain;
    
    	if(!(ptr = strstr(path, "./"))) return path;
    	if(ptr == path) path = path + 2;
    	remain = ptr + 2;
    	path = strncpy(path, path, strlen(path) - strlen(ptr));
    	path = strcat(path, remain);
    	return RemoveDot(path);
    }
    non so più come fare... dà segmentation fault...

    come lo sistemo? :master:
    Domanda... ma cosa intendi fare con quell'istruzione (in grassetto)? Stai confrontando due indirizzi di memoria... praticamente se l'indirizzo di memoria puntato da path è lo stesso di quello puntato da ptr, allora incrementi il puntatore path di 2 byte... :master:
    Se vuoi confrontare le stringhe devi usare strcmp, che ritorna 0 se le stringhe sono uguali...

    Poi, la penultima istruzione... strcat andrebbe utilizzata in modo diverso: il valore di ritorno di tale funzione, solitamente, lo si lascia perdere in quanto il risultato di tale istruzione (concatenzaione fra la prima e la seconda stringa) è già ritornato nella prima stringa:
    codice:
    strcat(str1, str2);
    str1 contiene la concatenazione fra str1 e str2...


    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

  8. #38
    con
    codice:
    if(ptr == path) path = path + 2
    se puntano lo stesso indirizzo, sposto l'inizio di path avanti di 2 caratteri... es: blabla diventa abla
    P.

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.