Autor Beitrag
Jakob_Ullmann
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1747
Erhaltene Danke: 15

Win 7, *Ubuntu GNU/Linux*
*Anjuta* (C, C++, Python), Geany (Vala), Lazarus (Pascal), Eclipse (Java)
BeitragVerfasst: Di 13.04.10 16:19 
Hallo!

Ich habe nun diesen Samstag Ubuntu installiert, mir CodeLite heruntergeladen und wollte nun mal vernünftig in C++ programmieren, wollte jedoch erstmal mit C anfangen. Ich habe jetzt schon ein paar Progrämmchen geschrieben, die mit Zahlen was machen, aber Strings gehen irgendwie noch nicht so. Ich wollte also folgendes compilieren:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
int main(void)
{
    char myname[5]; // erstmal mit Begrenzung ausprobieren
    printf("Name: \n");
    scanf(myname);
    printf("Willkommen %s", myname); // hier kommt der Fehler: Es gäbe kein String-Literal oder so
    return 0;
}


Da wollte ich mal fragen, wie das korrekterweise sein müsste, hier gibt es doch sicher viele, die sich auch mit C auskennen.
Jakob_Ullmann Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1747
Erhaltene Danke: 15

Win 7, *Ubuntu GNU/Linux*
*Anjuta* (C, C++, Python), Geany (Vala), Lazarus (Pascal), Eclipse (Java)
BeitragVerfasst: Mi 14.04.10 18:22 
OK, ich sehe gerade, dass das scanf(&myname) als Pointer sein muss. Aber trotzdem klappt das nicht. Keiner eine Idee?
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mi 14.04.10 21:59 
Bei dem Scanf fehlt das Muster für das Einlesen. Beachte, dass scanf leicht zu Buffer-Overflows führen kann. Das F in scanf steht für Formatstring; und genau der fehlt.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Chemiker
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 194
Erhaltene Danke: 14

XP, Vista 32 Bit, Vista 64 Bit, Win 7 64 Bit, Win 10, Win 11
BDS 2006, RAD Studio 2009+C++, Delphi 13, VS 2010 Prof.
BeitragVerfasst: Mi 14.04.10 23:25 
Hallo Jakob Ullmann,

ausblenden Quelltext
1:
scanf("%s", myname);					

dann sollte es eigentlich funktionieren.

Bis bald Chemiker
Jakob_Ullmann Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1747
Erhaltene Danke: 15

Win 7, *Ubuntu GNU/Linux*
*Anjuta* (C, C++, Python), Geany (Vala), Lazarus (Pascal), Eclipse (Java)
BeitragVerfasst: Do 15.04.10 15:26 
Hm, darauf hätte man doch eigentlich kommen können (naja, wie gesagt, hab bis jetzt nur was mit Zahlen gemacht, da will er ja komischerweise keinen Format-String, zumindest bei int und float)... Vielen Dank!