Add solutions for extended pointers.
This commit is contained in:
parent
5169496e00
commit
efa56b39be
60
06_erweiterte_zeiger/callByReference.c
Normal file
60
06_erweiterte_zeiger/callByReference.c
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* Was gibt folgendes Programm aus? Erst überlegen, dann ausführen.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define MAX_TEXT_LEN 100
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char text1[MAX_TEXT_LEN];
|
||||||
|
char *text2;
|
||||||
|
int number;
|
||||||
|
} MyStruct;
|
||||||
|
|
||||||
|
void modify1(MyStruct arg);
|
||||||
|
void modify2(MyStruct *arg);
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char text[MAX_TEXT_LEN] = "Text 2";
|
||||||
|
MyStruct someStruct = {"Text 1", text, 15};
|
||||||
|
|
||||||
|
printf("someStruct.text1 = %s\n", someStruct.text1);
|
||||||
|
printf("someStruct.text2 = %s\n", someStruct.text2);
|
||||||
|
printf("someStruct.number = %d\n", someStruct.number);
|
||||||
|
|
||||||
|
modify1(someStruct);
|
||||||
|
|
||||||
|
printf("\nNach modify1:\n");
|
||||||
|
printf("someStruct.text1 = %s\n", someStruct.text1);
|
||||||
|
printf("someStruct.text2 = %s\n", someStruct.text2);
|
||||||
|
printf("someStruct.number = %d\n", someStruct.number);
|
||||||
|
|
||||||
|
modify2(&someStruct);
|
||||||
|
|
||||||
|
printf("\nNach modify2:\n");
|
||||||
|
printf("someStruct.text1 = %s\n", someStruct.text1);
|
||||||
|
printf("someStruct.text2 = %s\n", someStruct.text2);
|
||||||
|
printf("someStruct.number = %d\n", someStruct.number);
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void modify1(MyStruct arg)
|
||||||
|
{
|
||||||
|
strcpy(arg.text1, "Neuer Text 1");
|
||||||
|
strcpy(arg.text2, "Neuer Text 2");
|
||||||
|
arg.number = 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
void modify2(MyStruct *arg)
|
||||||
|
{
|
||||||
|
strcpy(arg->text1, "Wieder neuer Text 1");
|
||||||
|
strcpy(arg->text2, "Wieder neuer Text 2");
|
||||||
|
arg->number = 200;
|
||||||
|
}
|
||||||
57
06_erweiterte_zeiger/stringtausch.c
Normal file
57
06_erweiterte_zeiger/stringtausch.c
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* Im folgenden Programm hat sich ein Fehler eingeschlichen. Eigentlich sollen die Zeichenketten vertauscht
|
||||||
|
* ausgegeben werden. Allerdings klappt etwas nicht.
|
||||||
|
*
|
||||||
|
* 1. Lassen Sie zunächst das Programm laufen und finden Sie heraus, warum die Zeichenketten string1 und string2 nicht vertauscht werden.
|
||||||
|
* 2. Sorgen Sie nun dafür, dass die Zeichenketten vertauscht werden. Ändern Sie dafür nur den Funktionsinhalt von tauscheStrings(...)
|
||||||
|
* 3. Schreiben Sie nun eine neue Tauschfunktion, die die ursprünglich angedachte Funktionalität umsetzt. Es sollen also nur die Zeiger vertauscht
|
||||||
|
* werden. Der Array-Inhalt soll unverändert bleiben.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define MAX_STRING_LEN 8
|
||||||
|
|
||||||
|
void tauscheStringsZgrZgr(char **zgr1, char **zgr2);
|
||||||
|
void tauscheStringsInhalt(char *zgr1, char *zgr2);
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char strings[][MAX_STRING_LEN] = {"String1", "String2"};
|
||||||
|
char *string1 = strings[0];
|
||||||
|
char *string2 = strings[1];
|
||||||
|
|
||||||
|
printf("Vor Tausch:\n");
|
||||||
|
printf("Inhalt von string1: %s\n", string1);
|
||||||
|
printf("Inhalt von string2: %s\n", string2);
|
||||||
|
printf("Inhalt von strings[0]: %s\n", strings[0]);
|
||||||
|
printf("Inhalt von strings[1]: %s\n", strings[1]);
|
||||||
|
|
||||||
|
//tauscheStringsZgrZgr(&string1, &string2);
|
||||||
|
tauscheStringsInhalt(string1, string2);
|
||||||
|
|
||||||
|
printf("\nNach Tausch:\n");
|
||||||
|
printf("Inhalt von string1: %s\n", string1);
|
||||||
|
printf("Inhalt von string2: %s\n", string2);
|
||||||
|
printf("Inhalt von strings[0]: %s\n", strings[0]);
|
||||||
|
printf("Inhalt von strings[1]: %s\n", strings[1]);
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void tauscheStringsZgrZgr(char **zgr1, char **zgr2)
|
||||||
|
{
|
||||||
|
char *tmp = *zgr1;
|
||||||
|
*zgr1 = *zgr2;
|
||||||
|
*zgr2 = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void tauscheStringsInhalt(char *zgr1, char *zgr2)
|
||||||
|
{
|
||||||
|
char tmp[MAX_STRING_LEN];
|
||||||
|
strcpy(tmp, zgr1);
|
||||||
|
strcpy(zgr1, zgr2);
|
||||||
|
strcpy(zgr2, tmp);
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user