problem with sizeof operator

This is a discussion on problem with sizeof operator within the C Programming forums, part of the General Programming Boards category; i have initialized an array using calloc. i can only pass the pointer to a function. in the function i ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    6

    problem with sizeof operator

    i have initialized an array using calloc.
    i can only pass the pointer to a function.
    in the function i need to find the length of the array. how do i do it??

    wen i use the sizeof() operator it always gives me the value 4 ?? i dont understnd.

    #include <stdio.h>
    #include <stdlib.h>

    int main() {
    double *array;

    array = (double *)malloc(10*sizeof(double));
    printf("size = %d",sizeof(array));
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    Quote Originally Posted by slash_axl
    in the function i need to find the length of the array. how do i do it??
    Pass the length of the dynamic array as an argument.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    And don't forget to free!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    is there any other method other than passing the length as an argument??

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    Not in C, no. You need to keep track of the length.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    Quote Originally Posted by slash_axl
    is there any other method other than passing the length as an argument??
    You could designate some special value to terminate the array, like how the null character is used for a string. However, there is then the danger of out of bounds access should you forget to actually place that value at the appropriate point in the array. Of course, this cannot be used if there is no suitable value, and then it potentially wastes a little space.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    thanx a lot!!!

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    And don't forget code tags either.
    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.

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also, no need to cast malloc, if you are using a C compiler.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  10. #10
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        double *array;
    
        array = (double *) malloc(10 * sizeof(double));
        printf("size = %d", sizeof(array));
        return 0;
    }
    Something to keep in mind as well is that you should get into the habit of checking the returned value from malloc(). If it returns NULL, and you dereference that, you run into trouble.

    As for what claudiu wrote [above] about casting malloc(), here are some links from FAQs:


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 06-28-2006, 02:58 AM
  2. Operator Overload problem
    By Kasatka in forum C++ Programming
    Replies: 3
    Last Post: 03-15-2004, 09:29 PM
  3. overload insert operator problem
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 04:34 PM
  4. problem with new operator
    By codefx in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2002, 06:04 PM
  5. A newby question on the sizeof operator
    By Evo in forum C++ Programming
    Replies: 2
    Last Post: 07-19-2002, 04:17 AM

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