Pagina 1 di 3 1 2 3 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 26
  1. #1

    [C] Array dinamici..un problema..

    Ho scritto questo codice che chiede la grandezza di un array(quindi dinamico) e in base alla grandezza immessa legge gli n elementi e infine li stampa a video(sempre quelli immessi)

    codice:
    #include <stdio.h>
    
    int i,n,j;
    
    int *vettore = new int[n];
    
    main()
    {
    
    printf("Inserisci la grandezza dell'array ");
    
    scanf("%d",&n);
    
    
    for (i=0; i<n; i++)
    
    scanf("%d",&vettore[i]);
    
    
    for (j=0; j<n; j++)
    
    printf("%d ",vettore[j]);
    
    }
    Viene compilato senza problemi ma quando è in esecuzione se metto ad es 3-4 come elementi max me li chiede correttamente,li inserisco.....e vengono stampati...

    Se metto tipo 12 elementi me ne chiede un po poi mi da un errore di protezione generale... (una cosa del genere mi sembra..ambiente windows xp pro...)

    Potete provarlo voi e farmi sapere??
    Che c'è di errato?

    grazie ciao
    Vediamo..sogni che diventano professione...passioni che diventano vita... Free as in Freedom...

  2. #2
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448
    che è quel "new int[n]"?

    codice:
    #include <stdio.h>
    
    int i,n,j;
    
    main()
    {
    printf("Inserisci la grandezza dell'array ");
    scanf("%d",&n);
    int *vettore= (int *) calloc(n,sizeof(int));
    for (i=0; i<n; i++)
    scanf("%d",&vettore[i]);
    for (j=0; j<n; j++)
    printf("%d ",vettore[j]);
    }
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

  3. #3
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448
    ah, dimenticavo, aggiungi un
    codice:
    free(vettore);
    in fondo.
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

  4. #4
    Utente di HTML.it L'avatar di infinitejustice
    Registrato dal
    Nov 2001
    residenza
    Barcelona
    Messaggi
    772
    Originariamente inviato da pdpmpd
    calloc(sizeof(int),n);
    nn vanno al contrario?
    Live fast. Troll hard.
    Pythonist | Djangonaut | Puppeteer | DevOps | OpenStacker | Lost in malloc
    Team Lead @Gameloft Barcelona

  5. #5
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448
    già, caxxarola, corretto.
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

  6. #6
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448
    ma tu lo sai cos'è quel "new int[n]"?
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

  7. #7
    Utente di HTML.it L'avatar di infinitejustice
    Registrato dal
    Nov 2001
    residenza
    Barcelona
    Messaggi
    772
    mah sembra un mix fra le sintassi c e c++ o java
    nn so nemmeno se new sia una keyword in c ansi
    Live fast. Troll hard.
    Pythonist | Djangonaut | Puppeteer | DevOps | OpenStacker | Lost in malloc
    Team Lead @Gameloft Barcelona

  8. #8
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448
    ti assicuro che non è standard (nn compila una ceppa).
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

  9. #9
    Infatti new è una keyword del c++.
    Comunque in ANSI C99 si può fare questo

    codice:
    #include <stdio.h>
    
    int main()
    {
      int i, n;
    
      printf("Inserisci la grandezza dell'array ");
      scanf("%d", &n);
    
      int vettore[n];
    
      for (i = 0; i < n; i++)
        scanf("%d", &vettore[i]);
      
      for (i = 0; i < n; j++)
        printf("%d ", vettore[j]);
    
      return 0;
    }
    gcc dalla 3 in poi supporta l'ANSI C99

  10. #10
    Utente di HTML.it L'avatar di pdpmpd
    Registrato dal
    Jan 2003
    Messaggi
    448
    Originariamente inviato da internet
    Infatti new è una keyword del c++.
    Comunque in ANSI C99 si può fare questo

    codice:
      int vettore[n];
    gcc dalla 3 in poi supporta l'ANSI C99
    sarà anche vero questo, ma io c'ho il gcc 3.2.2 e usando questo metodo mi da segfault (mentre allocando con calloc no).
    Drug misuse is not a disease, it is a decision, like the decision to step out in front of a moving car. [...] In this particular life-style the motto is "be happy now because tomorrow you are dying", but the dying begins almost at once, and the happiness is a memory.

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 © 2024 vBulletin Solutions, Inc. All rights reserved.