Jump to content

some queries on arrays

- - - - -

  • Please log in to reply
3 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi

1: Is this possible to have an array of zero size such as array[0]? What would it mean?

2: The instructor mentioned that it is not possible to return an array using a user-defined function (perhaps, this is true in the context of the techniques available to the beginners). I think he was also saying that it is possible to return one element of an array at a time.

3: Suppose
int LEN = 10
, then when I write
char name[LEN] = {0}
what does it really mean? The name array is supposed to contain characters but I'm using a constant value which is "0".
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
1: it might be possible, but would be completely useless. Have you tried creating one?
2: There are ways to do it, but it involves pointers.
3: 0 is the same as '/0', which is the terminator of a c-string.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
1) array[0] would point to a garbage location as there is no first element, so it is not useful.

2) The issue with returning an array is scope, the array will be defined within the function (unless on the heap with malloc()) and will be invalid after the function completes.

3) I have tried to explain this,

Quote

what does it really mean?
It will initialize the first element to zero, not the rest. However, as you had only initialized name[0], the rest must be initialized and defaults to zero. You can see = {0} as a shortcut to typing = {0,0,0,0,0,0,0,0,0,0,0,0...}

For example, this will not work:
char name[LEN] = {'a'};

It will only initialize name[0] to 'a', the rest will default to zero unless you state otherwise. memset could be used for example to set everything to a non-zero value for integral types.

Quote

The name array is supposed to contain characters but I'm using a constant value which is "0".

Please, remember that characters like 'a' and 'b' are really integers, they are ASCII code points. Therefor any value between 0..127 is a valid char.

You can think of char as a single byte integer, however with special treatment (we can use 'a' instead of remembering each ASCII codepoint, and use "abcde" instead of {'a','b','c',...}

Please feel free to ask for any clarification if I am unclear on some things.

Alexander.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
Regarding 1, there is still a clarification. There exists a scenario in which zero length arrays are used but it is pretty advanced use of this feature i.e.


typedef struct st

{

  int a;

  char arr[0]; // this basically tells the compiler to NOT include size of this in the structure initially. 

};


// latter we would do


st instance;

instance.arr = malloc(10);


This has a lot of simplicity and advantage when using size of such a structure in code. Details can be found at.
Zero Length - Using the GNU Compiler Collection (GCC)
Today is the first day of the rest of my life




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users