mogbo
Banned
Hallo,
habe ein etwas müßiges Problem, was ohne Fehler ausgegeben wird, ich mir aber nicht sicher bin ob es wirklich die richtige Lösung ist:
Mein Programm etwas vereinfacht:
Speziell dieser Teil:
Ich möchte das %s (current->array) die maximale Länge sizeof(current->array) hat, übergebe aber mit sizeof(current->array) einen unsigned long an %.*s (einen int?).
Ist meine Lösung eine gute Idee mit "(int) sizeof(current->array)" oder sollte ich mir einen anderen Wert einprägen?
ergibt:
Was wäre die Musterlösung?
habe ein etwas müßiges Problem, was ohne Fehler ausgegeben wird, ich mir aber nicht sicher bin ob es wirklich die richtige Lösung ist:
Mein Programm etwas vereinfacht:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
int zahl;
char array[55];
struct node * next;
};
struct node * head = NULL;
int
main(int argc, char * argv[])
{
head = malloc(sizeof(struct node));
struct node * current = head;
int i;
for (i = 0; i <= argc; i++) {
head->zahl = i;
snprintf(head->array, sizeof(head->array), "%s", argv[i]);
head->next = malloc(sizeof(struct node));
head = head->next;
}
head = NULL;
i = 0;
while (current->next != NULL && i < argc) {
if (current->zahl == 0) {
/*
* do nothing
*/
} else {
printf("[%i]\t%.*s\n", current->zahl, (int) sizeof(current->array), current->array);
}
current = current->next;
i++;
}
free(head);
return 0;
}
Speziell dieser Teil:
Code:
} else {
printf("[%i]\t%.*s\n", current->zahl, (int) sizeof(current->array), current->array);
}
Ist meine Lösung eine gute Idee mit "(int) sizeof(current->array)" oder sollte ich mir einen anderen Wert einprägen?
Code:
#include <stdio.h>
int
main(void)
{
printf("unsigned long: %lu\n", sizeof(unsigned long));
printf("int: %lu\n", sizeof(int));
return 0;
}
ergibt:
Code:
unsigned long: 8
int: 4
Was wäre die Musterlösung?