| | |
why use of size 0 or 1 array in structure for URI'S etc
![]() |
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
I give up! 1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ] 2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ... 3) What is the 123456789 prime numer? Ask4Answer
•
•
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 |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf scripting segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






