Why is sizeof(arrayname)=4 and not 67 in function?

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 |
@-----------------------------------@
 
J

Joona I Palaste

kimi said:
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);

filename is a char pointer. sizeof(filename) is therefore going to be
sizeof(char *), no matter *WHAT* you pass as the parameter's value,
dateiname or anything else. Because of how C's type system works, it
is impossible to get the array's true size through a pointer. You'll
have to use another way of getting the size of dateiname.
fgets(filename,sizeof(filename),stdin);/*here stops the Funktion*/

How about this?
fgets(filename, size, stdin);
I haven't tested it but your code looks trivial enough for it to work.
 
S

sellountos euripides

kimi said:
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.

- snip -
---------------------------
/*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() {
int main(void) {
- or -
int main (int argc, char **argv) {
char dateiname[67];

printf("\n\t\tTexzeile erfassen\n");
printf("\nSpeichern unter >");
hole_text(dateiname, sizeof(dateiname));
sizeof is a unary operator, so the parentheses are not needed.
it should be
hole_text(dateiname, sizeof dateiname);
..
..
.. return EXIT_SUCCESS;
}

/* 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);

sizeof filename returns the size of the pointer.
fgets(filename,sizeof(filename),stdin);/*here stops the Funktion*/
^^^^^^^^^^^^^^^^
fgets(filename, size, stdin);
if (*(strchr(filename,'\n')) == '\n')
(*(strchr(filename,'\n'))) = '\0';

}

in hole_text the "dateiname" decay to a pointer to char. You
explicitly have to pass the size of the array. You have done it
correctly...

cheers.

e.j.s.
 
K

Keith Thompson

kimi said:
Why is the size of a arraypointer in a function diffrent to the outside? In
both cases it is an integer.

The C FAQ is at <http://www.eskimo.com/~scs/C-faq/top.html>.

You've asked question 6.21, "Why doesn't sizeof properly report the
size of an array when the array is a parameter to a function?".
Reading all of section 6 will help you understand the answer. Reading
the entire FAQ will also be helpful, though you may have trouble with
some of it if you're just learning the language.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,139
Messages
2,570,807
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top