View Full Version : [c++] strcpy()
faccio cose incredibili e mi perdo per le ca@@ate...
m_strStringa = (char *)malloc (512);
... assegno i char a m_strStringa e poi
char ok[512];
strcpy(ok,m_strStringa[0]);//ERRORE!!
m_strStringa contiene "i caratteri" e per scanzionarli uno ad uno mi serve inserirli nel char ok e poi tramite for leggerli uno ad uno facendo naturalmente strlen(ok);... ma non me lo permette il compilatore...
error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *'
oppure devo leggere un carattere alla volta di m_Stringa[0];
...è da ridere eh?
Ziosilvio
02-10-2006, 09:28
m_strStringa = (char *)malloc (512);
Questo è C, non C++.
assegno i char a m_strStringa e poi
char ok[512];
strcpy(ok,m_strStringa[0]);//ERRORE!!
Se m_strStringa è un char*, allora m_strStringa[0] è un char.
Non puoi chiamare strcpy su un char* e un char: devi chiamarla su due char*.
P.S.: Hai la sign irregolare.
Questo è C, non C++.
Se m_strStringa è un char*, allora m_strStringa[0] è un char.
Non puoi chiamare strcpy su un char* e un char: devi chiamarla su due char*.
P.S.: Hai la sign irregolare.
char *ok;
strcpy(ok,m_Stringa[0]);
questo è ancora errore...
Devi cambiare il secondo parametro, non il primo: cannot convert parameter 2 from 'char' to 'const char *'
strcpy(ok, &m_Stringa[0]);
che è lo stesso di
strcpy(ok, m_Stringa);
Devi cambiare il secondo parametro, non il primo: cannot convert parameter 2 from 'char' to 'const char *'
strcpy(ok, &m_Stringa[0]);
che è lo stesso di
strcpy(ok, m_Stringa);
Esatto... è una cosa che non mi era mai capitata!!
char ok[512];
m_strStringa = (char *)malloc (512);
strcpy(ok, &m_strStringa[0]);
perfetto grazie kk3z
una cosa:
cosa fà di preciso: (char *)malloc (512);
... e se invece usassi l'opratore new. Con l'operatore new otterrei lo stessa cosa di: m_strStringa = (char *)malloc (512); ?
e se invece usassi l'opratore new. Con l'operatore new otterrei lo stessa cosa di: m_strStringa = (char *)malloc (512); ?m_strStringa = new char[512];
dà lo stesso risultato di
m_strStringa = (char *)malloc (512);
L'unica cosa è che poi devi fare un delete m_strStringa; e non usare la free() della libreria "C".
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.