Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8555815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:12:36+00:00 2026-06-11T15:12:36+00:00

I declare hws globally and try to return it in this method but I

  • 0

I declare hws globally and try to return it in this method but I get a pointer error. I was wondering if anyone knew why that is and could suggest a solution? Randomize just get a random number.

extern int hws[100][20];

int randomize()
{

    int value;
    int tru_fals = 0;
    value = -1;
    while ( tru_fals != 1 )
    {
        value = rand();
        if ( 0 < value )
        {
            if( 101 > value )
            {
                tru_fals = 1;
            }
        }
    }
    return value;

}
int *populate()
{

    int i, j;

    i = 0;
    j = 0;
    while( i < 20 )
    {
        while ( j < 100)
        {
            int temp = randomize();
            hws[i][j] = temp;
            j++;
        }
        i++;
    }
    return hws;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:12:37+00:00Added an answer on June 11, 2026 at 3:12 pm

    Except when it is the operand of the sizeof, _Alignof, or unary & operator, or is a string literal being used to initialize another array in a declaration, an expression of type “N-element array of T” will be converted (“decay”) to an expression of type “pointer to T“, and its value will be the address of the first element in the array (6.3.2.1/3).

    In the line return hws; in populate, the type of the expression hws is “100-element array of 20-element array of int“; by the rule above, it will be converted to type “pointer to 20-element array of int“, or int (*)[20]. Thus, the correct declaration for populate would need to be

    int (*populate())[20]
    {
       ...
       return hws;
    }
    

    which reads as

          populate           -- populate
          populate()         -- is a function
         *populate()         -- returning a pointer
        (*populate())[20]    -- to a 20-element array
    int (*populate())[20]    -- of int.
    

    and the type of whatever you return the result to would need to be int (*)[20] as well.

    Having said that…

    Using global variables this way is highly discouraged for a number of reasons. It would be better to pass the array in to populate as a parameter, like so:

    void populate(int (*arr)[20], size_t rows)
    {  
      size_t i, j;
    
      for (i = 0; i < rows; i++)
      {
        for (j = 0; j < 20; j++)
        {
          arr[i][j] = randomize();
        }
      }
    }
    

    You would call this as simply

    populate(hws, sizeof hws / sizeof hws[0]);
    

    If you’re using a compiler that supports variable length arrays (either a C99 compiler or a C2011 compiler that does not define __STDC_NO_VLA__ or defines it to 0), you could define the function as

    void populate(size_t cols, size_t rows, int (*arr)[cols]) // cols must be declared
    {                                                         // before it can be used
      size_t i, j;                                            // to declare arr
    
      for (i = 0; i < rows; i++)
      {
        for (j = 0; j < cols; j++)
        {
          arr[i][j] = randomize();
        }
      }
    }
    

    and call it as

    size_t rows = sizeof hws[0] / sizeof hws[0][0];  // dividing the size of the array
    size_t cols = sizeof hws / sizeof hws[0];        // by the size of the first element
    populate(cols, rows, hws);                       // gives the number of elements
    

    so you’re not hardcoding the 20 anywhere.

    If you aren’t using a compiler that supports variable length arrays, and you don’t want to hardcode the number of rows in the function prototype, you can do something like this:

    void populate(int *arr, size_t rows, size_t cols)
    {
      size_t i, j;
      for (i = 0; i < rows; i++)
      {
        for (j = 0; j < cols; j++)
        {
          arr[i * rows + j] = randomize();
        }
      }
    }
    

    and call it as

    // rows and cols calculated as above
    populate(&hws[0][0], rows, cols);
    

    In this case, instead of passing a pointer to the array, we pass a pointer to the first element (the address value is the same, but the type is int * instead of int (*)[20]. So the populate function treats it like a 1D array, and computes the offset with i * rows + j. Note that this trick will only work for 2D arrays that have been allocated contiguously.

    This also means that populate can work with arrays of any size, not just Nx20.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

declare function Error:toString($this as javaObject) as xs:string external; the previous binds a return String()
declare @mydata nvarchar(4000) set @mydata = '36|0, 77|5, 132|61' I have this data that
DECLARE @ID INT ; This statement parses fine with MS SQL Server, but gives
I declare array like that private double[] array = new double[length] . Is it
declare function tns:getDt($inp as xs:string) as element(ns1:Sschema)*{ let $primary := fn-bea:get-property('PRIMARY','1') let $sec :=
i declare a variable in one class and want to use that variable in
Why declare the class that you included as a header file? #include TreeCallObj.h #include
declare @test varchar(20) set @test = 'VALUE' exec(' select '+@test+' ') This returns: Invalid
DECLARE l_string NVARCHAR2(600) := '123456'; checksum NVARCHAR2(600); BEGIN DBMS_OUTPUT.DISABLE; DBMS_OUTPUT.ENABLE(1000000); DBMS_OBFUSCATION_TOOLKIT.md5 (input_string => l_string,
DECLARE @table table(XYZ VARCHAR(8) , id int) INSERT INTO @table SELECT '4000', 1 UNION

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.