473,549 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why is sizeof(arraynam e)=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
segmentationfau lt("Speicherzug riffsfehler").
Excidently i only typed to charakter in an it worked.

The problem is the sizeof(arraynam e), 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\tT exzeile erfassen\n");
printf("\nSpeic hern unter >");
hole_text(datei name, sizeof(dateinam e));
..
..
..
}

/* mkfunktions */
/* hole_text better takes array pointer and int arraysize*/

void hole_text(char *filename, int size) {

printf("\nEnter filename : ");

printf("\nsizeo f = %i, size = %i\n",sizeof(fi lename), size);

fgets(filename, sizeof(filename ),stdin);/*here stops the Funktion*/
if (*(strchr(filen ame,'\n')) == '\n')
(*(strchr(filen ame,'\n'))) = '\0';

}
--
@-----------------------------------@
| Michael Kindermann |
| Wuerzburg/Germany |
| kimi-at-wildewear.de |
@-----------------------------------@
Nov 14 '05 #1
3 1650
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
segmentationfau lt("Speicherzug riffsfehler").
Excidently i only typed to charakter in an it worked. The problem is the sizeof(arraynam e), 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\tT exzeile erfassen\n");
printf("\nSpeic hern unter >");
hole_text(datei name, sizeof(dateinam e));
.
.
.
} /* mkfunktions */
/* hole_text better takes array pointer and int arraysize*/ void hole_text(char *filename, int size) {

printf("\nEnter filename : ");

printf("\nsizeo f = %i, size = %i\n",sizeof(fi lename), 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(filen ame,'\n')) == '\n')
(*(strchr(filen ame,'\n'))) = '\0'; }


--
/-- Joona Palaste (pa*****@cc.hel sinki.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
segmentationfau lt("Speicherzug riffsfehler").
Excidently i only typed to charakter in an it worked.

The problem is the sizeof(arraynam e), 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\tT exzeile erfassen\n");
printf("\nSpeic hern unter >");
hole_text(datei name, sizeof(dateinam e)); sizeof is a unary operator, so the parentheses are not needed.
it should be
hole_text(datei name, 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("\nsizeo f = %i, size = %i\n",sizeof(fi lename), size);
sizeof filename returns the size of the pointer.

fgets(filename, sizeof(filename ),stdin);/*here stops the Funktion*/ ^^^^^^^^^^^^^^^ ^
fgets(filename, size, stdin);
if (*(strchr(filen ame,'\n')) == '\n')
(*(strchr(filen ame,'\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_Keit h) 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
4260
by: Ronald A. Andersen | last post by:
********** header **************** struct articleFile2 { char CR; int intStampDate; }; *********** source *************** struct articleFile2 *ptrArticle2 = NULL;
11
2077
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... My implementation seems to work great for primitive types, structures and unions, but I can't quite get an array of function-pointers working...
27
2451
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 the return value of ff(). The code below seems to work, but I would appreciate your comments. Have I got it right? Does the function name "decay" to...
2
1277
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
8332
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
3261
by: junky_fellow | last post by:
Hi, Is it possible to implement sizeof as a C function ?
2
1364
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, it returns that the size is 40 instead of 38. I consider that a short is 2 bytes, float 4 bytes, unsigned long 4 byte, unsigned short 2 bytes. If i...
3
1931
by: ravichobey | last post by:
Hi all, How to implement sizeof() function using C programming? Regards, Ravi
1
1586
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 what I am really interested in is both the type and the number of bytes a field is allocated (maximum permitted, not actually maximum used in a...
0
7546
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7471
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7740
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5387
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5111
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3517
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3496
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1962
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.