473,396 Members | 1,915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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 |
@-----------------------------------@
Nov 14 '05 #1
3 1637
kimi <ki**@wildwear.de> scribbled the following:
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.
if (*(strchr(filename,'\n')) == '\n')
(*(strchr(filename,'\n'))) = '\0'; }


--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"B-but Angus! You're a dragon!"
- Mickey Mouse
Nov 14 '05 #2
kimi wrote:
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.
Nov 14 '05 #3
kimi <ki**@wildwear.de> writes:
[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ronald A. Andersen | last post by:
********** header **************** struct articleFile2 { char CR; int intStampDate; }; *********** source *************** struct articleFile2 *ptrArticle2 = NULL;
11
by: Edd | last post by:
Hello all, I've made a data structure and an associated set of functions to enable me to store a dynamically-sized array of elements of whatever data type I like. Well that's the idea anyway......
27
by: Marlene Stebbins | last post by:
I am experimenting with function pointers. Unfortunately, my C book has nothing on function pointers as function parameters. I want to pass a pointer to ff() to f() with the result that f() prints...
2
by: Kevin Tang | last post by:
Dear All Is there any function in VB like "SizeOf()" in C/C++ ? Best Kevin Tang ( kevintang@ieee.org )
90
by: pnreddy1976 | last post by:
Hi, How can we write a function, which functionality is similar to sizeof function any one send me source code Reddy
15
by: junky_fellow | last post by:
Hi, Is it possible to implement sizeof as a C function ?
2
by: =?Utf-8?B?RnJhbg==?= | last post by:
sHi! I have a very strange problem with the behaviour of the sizeof command. I have tested it in vc6,vc 2001 and vc 2003. The problem is quite simple. When I made a sizeof to the next struct,...
3
by: ravichobey | last post by:
Hi all, How to implement sizeof() function using C programming? Regards, Ravi
1
by: Patient Guy | last post by:
I can find no PHP function that returns the maximum limit of field. For instance, when I see "varchar(256)" for the specification of a field, it's the number "256" I am interested in. Or perhaps...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.