Thread: memset zero 2d array big ISO warning ... how to rid of it?

  1. #1
    Banned
    Join Date
    Aug 2017
    Posts
    861

    memset zero 2d array big ISO warning ... how to rid of it?

    every sense I just put
    gcc -Wall -Wextra -Wpedantic -lm -o "%e" "%f"
    in my line I get this now.


    Code:
    gcc -Wall -Wextra -Wpedantic -lm -o "find_the_spywords" "find_the_spywords.c" (in directory: /home/userx/bin)
    find_the_spywords.c: In function 'main':
    find_the_spywords.c:62:4: warning: ISO C forbids zero-size array [-Wpedantic]
        memset(keyword, 0, 10 * 50 * sizeof(char[0][0]));
        ^
    find_the_spywords.c:62:4: warning: ISO C forbids zero-size array [-Wpedantic]
    find_the_spywords.c:85:2: warning: ISO C forbids zero-size array [-Wpedantic]
      memset(spywords, 0, 10 * 50 * sizeof(char[0][0]));
      ^
    find_the_spywords.c:85:2: warning: ISO C forbids zero-size array [-Wpedantic]
    Compilation finished successfully.
    so what is the real way to properly zero a 2d char array , or any 2d array for that matter?
    Code:
     char spywords[10][50];
     memset(spywords, 0, 10 * 50 * sizeof(char[0][0]));

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    If the compiler knows the size of the array, you can do:
    Code:
    memset(spywords, 0, sizeof(spywords));
    This code of course wouldn't work with dynamically allocated arrays. The size needs to be known at compile-time. An exception to this though are variable-length arrays, for which sizeof is evaluated at run-time.
    Devoted my life to programming...

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by GReaper View Post
    If the compiler knows the size of the array, you can do:
    Code:
    memset(spywords, 0, sizeof(spywords));
    This code of course wouldn't work with dynamically allocated arrays. The size needs to be known at compile-time. An exception to this though are variable-length arrays, for which sizeof is evaluated at run-time.
    thanks and,
    humm ( something to think about )to the second part...

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It would perhaps be preferred to do for (size_t i = 0; i < rowsize; i++) memset(spywords[i], 0, colsize); especially if the data structure is on the heap.

    Since a C string is defined as a sequence followed by literal zero as well, you could also make a similar loop and simply assign spywords[i][0] = '\0';

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by whiteflags View Post
    It would perhaps be preferred to do for (size_t i = 0; i < rowsize; i++) memset(spywords[i], 0, colsize); especially if the data structure is on the heap.

    Since a C string is defined as a sequence followed by literal zero as well, you could also make a similar loop and simply assign spywords[i][0] = '\0';
    that is what I am accustom to, initializing array in a loop. but with this new fanged memset, I may have gone a little memset happy.
    thanks for your input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 25
    Last Post: 10-25-2015, 01:37 AM
  2. Array warning that i can't figure out please help!!!!!
    By DJKnight2 in forum C Programming
    Replies: 8
    Last Post: 11-27-2011, 09:30 PM
  3. warning: excess elements in array initializer
    By redruby147 in forum C Programming
    Replies: 6
    Last Post: 09-30-2009, 06:08 AM
  4. warning: array ‘’ assumed to have one element
    By kiros88 in forum C Programming
    Replies: 6
    Last Post: 08-11-2009, 01:14 PM
  5. pass array to memset
    By sef0 in forum C Programming
    Replies: 5
    Last Post: 05-26-2009, 09:37 AM

Tags for this Thread