|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Junior Member
Iscritto dal: May 2012
Messaggi: 8
|
[C] funzione per allocare matrice dinamicamente
Codice:
#include <stdio.h>
#include <stdlib.h>
int** alloca(int,int);
main()
{
int **mat,i,j;
mat=alloca(2,2);
for(i=0;i<2;++i)
{
for(j=0;j<2;++j)
mat[i][j]=0;
}
}
int** alloca(int r,int c)
{
int **matrix,i;
matrix = (int**)malloc(r*sizeof(int*));
for (i=0; i<r; i++)
matrix[i] = (int*)malloc(c*sizeof(int));
return matrix;
}
ciao ragazzi! sto provando a scrivere una funzione che allochi dinamicamente una matrice di n righe ed m colonne. compilando ottengo gli errori: test2.c:6:21: error: macro "alloca" passed 2 arguments, but takes just 1 test2.c:6: error: ‘alloca’ redeclared as different kind of symbol test2.c:15:16: error: macro "alloca" passed 2 arguments, but takes just 1 test2.c:26:25: error: macro "alloca" passed 2 arguments, but takes just 1 test2.c:27: error: syntax error before ‘{’ token test2.c:30: error: ‘r’ undeclared here (not in a function) test2.c:30: warning: initialization makes integer from pointer without a cast test2.c:30: error: initializer element is not constant test2.c:30: warning: data definition has no type or storage class test2.c:31: error: syntax error before ‘for’ la linea 6 è quella relativa la dichiarazione della funzione alloca int** alloca(int,int); Qualcuno riesce ad aiutarmi? : - ( |
|
|
|
|
|
#2 |
|
Member
Iscritto dal: Oct 2010
Città: Savona
Messaggi: 194
|
Prova così:
Codice:
#include <stdio.h>
#include <stdlib.h>
int* allocare(int maxx,int maxy);
int main()
{
int *mat,i,j,maxx,maxy;
maxx=2;
maxy=2;
mat=allocare(maxx,maxy);
// errore non posso allocare nella Heap la matrice
if ( NULL == mat) return 0;
for(i=0;i<maxx,i++) //incremento dopo i
{
for(j=0;j<maxy;j++) //incremento dopo j
*(mat+(j+i*maxy))=0; // mat[i][j]=0; EDIT: Errore grave mio corretto successivamente
}
return 0;
}
int* allocare(int maxx,int maxy)
{
int *matrix;
// alloco uno spazio di memoria pari a max righe per il numero di max colonne
matrix = (int *)malloc(sizeof(int)*(maxx*maxy));
return matrix;
}
Comunque se ne ho fatti scusatemi perche ho tirato il codice giù a memoria. EDIT: ho riguardato il codice he come avevo anticipato ho fatto degli errori di concetto.
__________________
AMD Phenom II X6 3Ghz, MB Gigabyte 790xta-ud4 F4A, 4Gb Ram ddr3, HD Segate Barracuda 1T, SV ASUS GTX 560 Ti DirectCu II ,Monitor Philips 1920x1080 led 2 ms, Xbox360 Slim, Tablet Asus Transformer Pad TF300T, Gameboy Advance, Ipod Touch 16Gb Ultima modifica di AllerITA : 06-05-2012 alle 07:35. |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Jul 2008
Città: Roma
Messaggi: 542
|
|
|
|
|
|
|
#4 |
|
Junior Member
Iscritto dal: May 2012
Messaggi: 8
|
grazie mille a entrambi ragazzi!
a presto!! |
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: Feb 2006
Città: Parma
Messaggi: 3010
|
man alloca: funzione di allocazione piuttosto particolare (alloca dinamicamente nello stack frame), non POSIX, di origine probabilmente BSD-iana.
__________________
~Breve riferimento ai comandi GNU/Linux (ormai non molto breve...) |
|
|
|
|
|
#6 |
|
Member
Iscritto dal: Oct 2010
Città: Savona
Messaggi: 194
|
UP ho corretto un errore grave e fuorviante che potrebbe portare dei problemi al programma.
Codice:
//è corretto cosi:
*(mat+(j+i*maxy))=0; // mat[i][j]=0
__________________
AMD Phenom II X6 3Ghz, MB Gigabyte 790xta-ud4 F4A, 4Gb Ram ddr3, HD Segate Barracuda 1T, SV ASUS GTX 560 Ti DirectCu II ,Monitor Philips 1920x1080 led 2 ms, Xbox360 Slim, Tablet Asus Transformer Pad TF300T, Gameboy Advance, Ipod Touch 16Gb |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 22:37.




















