Thread: whats the difference

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    whats the difference

    i have the following snippet of code
    Code:
    char pword[] = "hel";
    
    int plen = strlen( pword );// plen = 3
    .....
    int guesses[plen][26] = { 0 };
    ....
    i get an error that guesses may not be initialized
    however if i have
    Code:
     int guesses[3][26] = { 0 }
    no error

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    In C the sizes of arrays must be compile time constants. If you want to use non-compile-time constants for the array size you must use manual memory management with malloc/free.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    @jim, You are forgetting about variable-length arrays (which are only optionally available pre-C23).

    @D.B., The second has constant dimensions whereas the first uses a variable.
    You are not allowed to initialize a variable-length array (I'm not sure why).
    However, you can simply use
    Code:
        memset(guesses, 0, sizeof guesses);  // include <string.h>
    which is basically what the zero initialization automatically does anyway.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    I take it you have dubbed me D.B. Cooper in your mind. Its along story but cooper came from Alice Cooper which used to cause great confusion as i am male but D.B is fine as its actualy my initials backwards!

    in reference to the thread in the end i just did what you did and used sizeof pword which is what i was cycling through anyways

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    @jim, You are forgetting about variable-length arrays (which are only optionally available pre-C23).
    Actually I'm not forgetting, I just consider VLA an abomination.

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by jimblumberg View Post
    Actually I'm not forgetting, I just consider VLA an abomination.
    At least one nice thing came with VLA support: you can pass array dimensions as function parameters. For example:

    Code:
    int foo(int x, int y[][x]);
    And now you can pass any 2-dimensional array of int to foo.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. whats the difference
    By lilhawk2892 in forum C++ Programming
    Replies: 2
    Last Post: 07-03-2006, 07:08 PM
  2. whats the difference between
    By InvariantLoop in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2004, 11:23 AM
  3. whats the difference???
    By whathetin in forum C++ Programming
    Replies: 6
    Last Post: 10-07-2003, 05:10 PM
  4. whats the difference?
    By phptech in forum C Programming
    Replies: 2
    Last Post: 06-19-2003, 08:25 PM
  5. Whats the difference?
    By electrolove in forum C Programming
    Replies: 7
    Last Post: 02-06-2003, 12:10 AM

Tags for this Thread