sizoof() operator

This is a discussion on sizoof() operator within the C Programming forums, part of the General Programming Boards category; the following code gives a linking error. what is the proces of linking? i want the size of pointer array ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    32

    sizeof() operator

    the following code gives a linking error.
    what is the proces of linking?

    i want the size of pointer array using sizeof() operator.

    //code
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    char *month[6]={"INVALID","JAN","FEB","MAR","MAY","JUNE"};
    printf("\n\t%d",sizeof(month));
    getch();
    }
    Last edited by samirself; 01-10-2005 at 09:29 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    It's called sizeof

    Also, main is spelled
    int main
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    with sizeof you will get size of month.
    You can make it like this.

    I hope I answer your question,

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    char *month[6]={"INVALID","JAN","FEB","MAR","MAY","JUNE"};
    int size;
    size = sizeof(month);
    size = size - 1;
    printf("\n\t%d", size);
    system("PAUSE");
    }
    ---------------------------
    Sorry for spelling errors, not English.

    Rugby Is Life!

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Try again xxxrugby. That code will give the size of a char*, not the length of the array. You cannot use sizeof to return the length of an array.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Actually you can pianorain. If xxxrugby hadn't subtracted 1 from the size it would have give 6 * sizeof(char *). Now that would be the size of the array. If you want the number of non null characters in the array that is a different story.

    samirself: What do you want? The size of the pointer or the number of non null characters in the desired month?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    > You cannot use sizeof to return the length of an array.
    Sure you can
    Code:
    int num_elements = sizeof(array) / sizeof(array[0]);
    works for any true array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Ha...my bad. *smacks self with trout* I didn't know sizeof would do that.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Hmmm looking at his original question... I am uncertain of what the linker error is, actually. It could have something to do with your void main (which i know you will have return 0 at the end of ).

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Well, it's hard to tell since the OP doesn't even tell us which compiler they're using. It's probably the use of getch(). At any rate, void main won't create a linker error because the linker couldn't care less about the return type of the function, it just cares about symbols and where they are.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 01:14 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 07:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

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