: Ich bin c & ich eine neue Ausgabe traf ..calloc nicht zugreifen können aus einem anderen .c File-
file1.c-
#include <stdio.h>
#include <stdlib.h>
extern int sec();
char *ptr=NULL;
int main(){
char *ptr=NULL;
ptr=(char*)calloc(sizeof(char),8);/*8 chars.*/
*(ptr+0)='0'; /*first char set to 0.*/
printf("%c\n",*ptr);
*(ptr+0)='r';
*(ptr+1)='o';
*(ptr+2)='i';
*(ptr+3)='L';
printf("%c %c %c %c \n",*(ptr+0),*(ptr+1),*(ptr+2),*(ptr+3));
sec();
return 0;}
Und file2.c-
#include <stdio.h>
#include <stdlib.h>
extern char *ptr;
void sec(void){
puts("before.");
*(ptr+0)='L';/*CARSH HERE.*/
*(ptr+1)='i';
*(ptr+2)='o';
*(ptr+3)='r';
puts("after.");
printf("%c %c %c %c ",*(ptr+0),*(ptr+1),*(ptr+2),*(ptr+3));
free(ptr);}
Ich habe so etwas schon einmal gemacht (mit einem Unterschied), aber jetzt stürzt es ab. Was ist der Grund? Wenn ich eine Funktion in der Quelldatei erstellen werde, wo der Speicher zugewiesen wurde, wird es gelöst?.
Siehe [Variable shadowing] (https://en.wikipedia.org/wiki/Variable_shadowing). – jweyrich