Confused !!!

This is a discussion on Confused !!! within the C Programming forums, part of the General Programming Boards category; i am totally confused. It is writen in books that memory is alocated in case of definition and not declaration. ...

  1. #1
    Unregistered
    Guest

    Angry Confused !!!

    i am totally confused.
    It is writen in books that memory is alocated in case of definition and not declaration.
    but when we write
    int i;//declaration
    still the memory is allocated..pls guide me
    Also in character arays the terminating character is '\0'(NULL).then what is the terminating character in integer arrays ?????

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    14

    Cool LET ME TELL YOU WHAT REALLY HAPPENS!

    first your second question,

    in any kind or type of array remember that the terminating char. is always NULL which is '\0'..
    here's the proof for that..
    try this program for that..

    void main()
    {
    int a[] = {1,2,3,4,5,6,7};
    int i;

    for(i=0;a[i]!='\0';i++)
    printf("%d",a[i]);

    getch();
    }


    now your first question.
    one thing you must know is that memory is always there,but you are only allowed to access your memory by reserving with variables.

    here declaration and defination is tottaly different.

    see i have 128 MB of memory.

    but when i write " int i " i am having 2 bytes reserved for me from the total memory.

    here's your the sentense

    "memory is alocated in case of definition and not declaration. "


    means that you are telling the compiler that what kind of data you are going to store in the memory.


    if int then 2 bytes
    if char then 1 byte
    if float then 4 byte
    if long float then 10 bytes..

    etc..
    Ruchit,
    think big,think ahead,
    go big,go ahead,

    ISN'T IT TOO FUNNY ABOUT MICROSOFT WINDOWS,THAT
    WHENEVER YOU WANT TO DO SOMETHING YOU CLICK ON "START" BUTTON AND THE FIRST OPTION YOU ARE HAVING IS
    "SHUT DOWN"...

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    Re: LET ME TELL YOU WHAT REALLY HAPPENS!

    Originally posted by s_ruchit
    first your second question,

    in any kind or type of array remember that the terminating char. is always NULL which is '\0'..
    here's the proof for that..
    try this program for that..

    void main()
    {
    int a[] = {1,2,3,4,5,6,7};
    int i;

    for(i=0;a[i]!='\0';i++)
    printf("%d",a[i]);

    getch();
    }
    Kids..... don't try this at home.

    The null character is only used for character arrays and not for integer arrays.
    Therefore the above example will not work.
    The null character is in fact the same as the number 0, so you can't use the null character to terminate an integer array.

    B.t.w.

    void main is wrong, use int main
    the size of int depends on the OS you are using (4 bytes for win32)

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    85
    Monster is almost rite!
    I want to add some more..
    Base on my experience with C

    int Arr[] = {1,2,3,4,5,6,7};
    In this scenerio, TERMINATE Value is garbage.(chunk)

    int Array[8]= {1,2,3,4,5,6,7};
    In this scenerio, if you try to create an extra
    cell, Array[7] without initialize a value to it, then it automaticly put a ZERO (0) to it.
    Unfortunately, C compiler don't check the boundary of array. This is a weakness in C and also not
    strong type checking programming language.
    Look, you can do this in C:
    int A[]= {1,2,3};
    int i;
    for (i= 0; i< 10; ++i) /* i suppose to be less than 3 (i <3)
    printf("%d\n", A[i]);

    Interesting huh??

    DV009

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by dv007
    Monster is almost rite!
    I want to add some more..
    Then what part is wrong!

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > int i;//declaration
    > still the memory is allocated..pls guide me
    If all you provide is
    int i;

    The compiler/linker will create
    int i = 0;
    for you, and it's this which creates the definition, and causes space to be allocated.

    If you have Kernighan and Richie "The C Programming Language 2nd Ed" to hand, then A10.2 explains.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused with array size
    By desmond5 in forum C Programming
    Replies: 4
    Last Post: 12-04-2007, 04:14 PM
  2. New to C++ and confused by boolean and ifs and else
    By jconner in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2006, 03:29 AM
  3. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 12:13 AM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 10:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21