| | |
why use of size 0 or 1 array in structure for URI'S etc
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
0
#2 Oct 12th, 2009
Array indexes start at 0.
char arrURI[1]; -- creates an array of size one
-- so there are two indexes 0 and 1
char data[0]; -- creates an initially empty array
Hope that is what you were looking for.
char arrURI[1]; -- creates an array of size one
-- so there are two indexes 0 and 1
char data[0]; -- creates an initially empty array
Hope that is what you were looking for.
Last edited by Grn Xtrm; Oct 12th, 2009 at 10:56 am.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
URL on facebook!
2
#3 Oct 12th, 2009
If the array is the last one in the struct, you are probably seeing the struct hack. Prior to C99 it is an unportable trick for allocating arrays in a struct:
The idea is that by tacking on extra memory and accessing it by overflowing the last array member, you can save an extra call to malloc:
C99 makes the struct hack a legal feature of the language, but I do not think it is a good idea even then because it makes the code less clear.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { char s[1]; } String; int main() { /* allocate 15 extra bytes for the string */ String *s = malloc(sizeof *s + 15); strcpy(s->s, "test string"); puts(s->s); free(s); return 0; }
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { char *s; } String; int main() { String *s = malloc(sizeof *s); s->s = malloc(15); strcpy(s->s, "test string"); puts(s->s); free(s->s); free(s); return 0; }
-Tommy (For Great Justice!) Gunn
•
•
Join Date: Dec 2007
Posts: 4
Reputation:
Solved Threads: 0
0
#4 Oct 13th, 2009
•
•
•
•
Array indexes start at 0.
char arrURI[1]; -- creates an array of size one
-- so there are two indexes 0 and 1
char data[0]; -- creates an initially empty array
Hope that is what you were looking for.
Thank you very much for responding .
As I understand.
from the example I have given
char arrURI[1] ;
is same to declaring
char arrURI;
BOTH allocates only one byte.
But in real usage arrURI is assigned some string which is more than one byte when it is declared as char arrURI[1].
In char data[0];
data is a constant pointer which cannot be assigned another address and data has no size . So how is it equivalent as creating an empty array.
Hope my question is clear.
1
#5 Oct 13th, 2009
•
•
•
•
char arrURI[1]; -- creates an array of size one
-- so there are two indexes 0 and 1
A[0] = 'a'; //ok
A[1] = 'b'; //bug
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson] 2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...[*] [*solved by : murtan] 3) What is the 123456789 prime numer?
•
•
Join Date: May 2008
Posts: 33
Reputation:
Solved Threads: 4
1
#6 Oct 13th, 2009
In the case of zero sized array, it was generally used as an easy way to index into a variable sized buffer ...
It lets the compiler calculate the offset so you don't have to.
C Syntax (Toggle Plain Text)
struct Header { DWORD dwMagic; DWORD dwSize; BYTE data[]; }; void MyFunction() { Header *pHdr = (Header *)buffer; Read(buffer, MAX_SIZE); switch(pHdr->dwMagic) { case M_TAG('R','I','F','F') : HandleRIFF(pHdr->data,pHdr->dwSize); break; // ... etc } }
It lets the compiler calculate the offset so you don't have to.
Last edited by SVR; Oct 13th, 2009 at 12:49 pm.
0
#7 Oct 13th, 2009
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
URL on facebook!
0
#9 Oct 14th, 2009
•
•
•
•
Why can't we just use a pointer.
sizeof . I think the original struct hack uses an array size of 0, and it evolved to use 1 for compilers that do not allow an array size of 0. -Tommy (For Great Justice!) Gunn
•
•
Join Date: May 2008
Posts: 33
Reputation:
Solved Threads: 4
0
#10 Oct 14th, 2009
•
•
•
•
Why can't we just use a pointer. It will do the same
eg : BYTE *byte;
C Syntax (Toggle Plain Text)
struct Header { DWORD dwMagic; DWORD dwSize; BYTE *data; }; void MyFunction() { Header *pHdr = (Header *)buffer; Read(buffer, MAX_SIZE); switch(pHdr->dwMagic) { case M_TAG('R','I','F','F') : HandleRIFF(pHdr->data,pHdr->dwSize); break; // ... etc } }
Now this will crash. Why ?
![]() |
Similar Threads
- size of array (C++)
- How to create a byte array/structure (C++)
- Size of array passed as parameter. (C++)
- Max size of Array (C++)
- how to increase the size of an array? (C)
Other Threads in the C Forum
- Previous Thread: 2d-board algorithm: Total number of different moves to reach a location
- Next Thread: Adding 2 arrays together
| Thread Tools | Search this Thread |
* ansi api append array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlogicaldrivestrin givemetehcodez grade graphics gtkwinlinux histogram homework i/o ide inches include infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft multi mysql oddnumber open opendocumentformat openwebfoundation overwrite pdf pointer pointers posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string strings suggestions test testautomation threads unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






