Nulling an array

This is a discussion on Nulling an array within the C Programming forums, part of the General Programming Boards category; I have an array: int array[50]; I want to null all the positions in the array because I will be ...

  1. #1
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463

    Nulling an array

    I have an array:

    int array[50];

    I want to null all the positions in the array because I will be copying some stuff later into it, but at the same time, not all the positions maybe filled. So later when I do a check to see how many integers are in there I can be assured that the checking will stop when it encounters a NULL.

    However when I do try to assign NULL

    array[n++] = NULL;

    I get warning assignment makes integer from pointer without a cast.

    What do I do?
    =========================================
    Everytime you segfault, you murder some part of the world

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Initialise the array to zeroes
    Code:
    int array[50] = {0};
    Of course, this implies that you cannot store zeroes in the array. An alternative is to have a counter keep track of how many elements are in use, but then you must use elements contiguously.
    Last edited by laserlight; 04-16-2008 at 07:59 AM.
    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
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by laserlight View Post
    Initialise the array to zeroes
    Code:
    int array[50] = {0};
    Ah...hehe
    =========================================
    Everytime you segfault, you murder some part of the world

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Or, if you need to do it again on an existing array, memset() is the function to use:
    Code:
    #include <stdlib.h>
    
    ...
    
    
       memset(array, 0, sizeof(array)); 
    ...
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    memset? Never heard of it, but I'll check it up, sounds useful.

    Thanks laser and mats
    =========================================
    Everytime you segfault, you murder some part of the world

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Here's the description:
    http://www.hmug.org/man/3/memset.php

    Edit: Also, NULL should be used ONLY on pointer variables - integer arrays should contain 0 (zero), NULL is "never" the right value for an integer - particularly in C (in C++ NULL is actually defined as 0, so it should make no difference to 0, but the meaning is stilll [of sorts] a NULL pointer).
    --
    Mats
    Last edited by matsp; 04-16-2008 at 08:05 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Err, I did that that initialize to 0 thing and it gave me 150 errors, quite literally.

    Could it be because this array is declared inside a struct?
    =========================================
    Everytime you segfault, you murder some part of the world

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You did WHAT?

    Yes, initializing an array inside a struct would be something like:
    Code:
    struct a
    {
       int array[50];
       int x;
       float f;
    };
    
    struct a somevar = { { 0 } , 7, 13.5f };  // Array set to all zeros, x = 7, f = 13.5
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Ok, I'll try that, but I really didn't think that it would bring up so many errors
    =========================================
    Everytime you segfault, you murder some part of the world

  10. #10
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    No that didn't work. However I tried initializing it in a function instead,

    Code:
    if (x >= 19) {
       h->array[A] = {0};
    }
    That quite literally gave me 100's of errors too.
    =========================================
    Everytime you segfault, you murder some part of the world

  11. #11
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Oh no wait, hang on that did work, but it takes me back to square one, integer from pointer without a cast warning
    =========================================
    Everytime you segfault, you murder some part of the world

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    When you get "150 errors" from one small change, it's usually because the compiler got completely confused, and lost track of what it was doing, and then "everything" generates an error, because it's all "out of place".

    I liken this with when you tell someone that they should turn right after the big tree, then take the first left, and someone has cut down and removed the tree you referred to, so the person following your directions will drive way past the first turning, and then be completely lost at the point when the person actually finds a turning next to a big tree. Likewise, if the compiler gets confused by certain things and "takes the wrong turning", it can be VERY easy to run up hundreds of errors, because it's just lost the plot completely.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by matsp View Post
    When you get "150 errors" from one small change, it's usually because the compiler got completely confused, and lost track of what it was doing, and then "everything" generates an error, because it's all "out of place".

    I liken this with when you tell someone that they should turn right after the big tree, then take the first left, and someone has cut down and removed the tree you referred to, so the person following your directions will drive way past the first turning, and then be completely lost at the point when the person actually finds a turning next to a big tree. Likewise, if the compiler gets confused by certain things and "takes the wrong turning", it can be VERY easy to run up hundreds of errors, because it's just lost the plot completely.

    --
    Mats
    Nah, your struct initializer method worked, but it still gives the warning I had to begin with.

    As a matter of fact, it compounds the error, because I got a pointer array and it's giving me a warning for that as well. Isn't there a way to change just one variable in a struct without needing to include others?
    Last edited by JFonseka; 04-16-2008 at 08:27 AM.
    =========================================
    Everytime you segfault, you murder some part of the world

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    if (x >= 19) {
       h->array[A] = {0};
    }
    What's A - and you probably shouldn't have a {0} there - this is not a initialization of a variable, it's an assignment - so the you can't/shouldn't use braces here - it will just cause the compiler to get confused.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by matsp View Post
    Code:
    if (x >= 19) {
       h->array[A] = {0};
    }
    What's A - and you probably shouldn't have a {0} there - this is not a initialization of a variable, it's an assignment - so the you can't/shouldn't use braces here - it will just cause the compiler to get confused.

    --
    Mats
    A is #defined to be 50
    =========================================
    Everytime you segfault, you murder some part of the world

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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