K
kimi
Hello,
yes i try to learn c-programming.So i have a question on my code, but if i
am wrong here please tell me a group where i belong to.
I wrote a function which fgets a filename from the commandline. It worked
great when i put the code in main(). Then why not, copy this piece in a
function. But when i type in the text, pressed enter i got an
segmentationfault("Speicherzugriffsfehler").
Excidently i only typed to charakter in an it worked.
The problem is the sizeof(arrayname), which is inside the function the size
of the function.
I thougt that making array dateiname[67] global would solve the problem, but
it doesnt help.
I solved my problem by putting the size as an Argument into the function.
Why is the size of a arraypointer in a function diffrent to the outside? In
both cases it is an integer.
Why doesnt work the global variable?
Where should i start to think?
output of Programm :
Dateiname eingeben :
sizeof = 4, size = 67
---------------------------
/*bsp073.c*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void hole_text(char *filename, int size);
/*char dateiname[67]; this doesnt help*/
main() {
char dateiname[67];
printf("\n\t\tTexzeile erfassen\n");
printf("\nSpeichern unter >");
hole_text(dateiname, sizeof(dateiname));
..
..
..
}
/* mkfunktions */
/* hole_text better takes array pointer and int arraysize*/
void hole_text(char *filename, int size) {
printf("\nEnter filename : ");
printf("\nsizeof = %i, size = %i\n",sizeof(filename), size);
fgets(filename,sizeof(filename),stdin);/*here stops the Funktion*/
if (*(strchr(filename,'\n')) == '\n')
(*(strchr(filename,'\n'))) = '\0';
}
--
@-----------------------------------@
| Michael Kindermann |
| Wuerzburg/Germany |
| kimi-at-wildewear.de |
@-----------------------------------@
yes i try to learn c-programming.So i have a question on my code, but if i
am wrong here please tell me a group where i belong to.
I wrote a function which fgets a filename from the commandline. It worked
great when i put the code in main(). Then why not, copy this piece in a
function. But when i type in the text, pressed enter i got an
segmentationfault("Speicherzugriffsfehler").
Excidently i only typed to charakter in an it worked.
The problem is the sizeof(arrayname), which is inside the function the size
of the function.
I thougt that making array dateiname[67] global would solve the problem, but
it doesnt help.
I solved my problem by putting the size as an Argument into the function.
Why is the size of a arraypointer in a function diffrent to the outside? In
both cases it is an integer.
Why doesnt work the global variable?
Where should i start to think?
output of Programm :
Dateiname eingeben :
sizeof = 4, size = 67
---------------------------
/*bsp073.c*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void hole_text(char *filename, int size);
/*char dateiname[67]; this doesnt help*/
main() {
char dateiname[67];
printf("\n\t\tTexzeile erfassen\n");
printf("\nSpeichern unter >");
hole_text(dateiname, sizeof(dateiname));
..
..
..
}
/* mkfunktions */
/* hole_text better takes array pointer and int arraysize*/
void hole_text(char *filename, int size) {
printf("\nEnter filename : ");
printf("\nsizeof = %i, size = %i\n",sizeof(filename), size);
fgets(filename,sizeof(filename),stdin);/*here stops the Funktion*/
if (*(strchr(filename,'\n')) == '\n')
(*(strchr(filename,'\n'))) = '\0';
}
--
@-----------------------------------@
| Michael Kindermann |
| Wuerzburg/Germany |
| kimi-at-wildewear.de |
@-----------------------------------@