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

  • Home
  • SEARCH
  • 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 6686969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:14:00+00:00 2026-05-26T05:14:00+00:00

Need to have an array full of random numbers within range. Code: void fillArray(int

  • 0

Need to have an array full of random numbers within range. Code:

void fillArray(int *arr){
        for(int i=0;i<(sizeof arr);i++){
            arr[i] = rand() % 2 - 2;
            }
        }

int *arrPoint(int *arr, int max){
    for (int i=0;i<max;i++){
        printf("%d is %d\n",i,arr[i]);
    }
    return arr;
}

int main(int argc, char *argv[]){
    srand ( time(NULL) );
    int arr_f[15];
    fillArray(arr_s);
    arrPoint(arr_s, 15);
    system("PAUSE");
    return EXIT_SUCCESS;
}    

Output:

    0 is -2
1 is -1
2 is -2
3 is -1
4 is 0
5 is 0
6 is 4633240
7 is 2686652
8 is 1973724226
9 is 1974338216
10 is 2686716
11 is 1973744850
12 is 8
13 is 1973752206
14 is 1973752162
Press any key to continue . . .

What the hell? Putting rand() % 2 into brackets doesn’t help either. What these numbers are and how do I get rid of them?

P.S. Tried this in crappy Dev-C++ and Code::Blocks with the same result. Need the program to be small sized (putting it to dropbox), so no, I can’t use 100mb boost lib.

  • 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-05-26T05:14:01+00:00Added an answer on May 26, 2026 at 5:14 am
    void fillArray(int *arr){
        for(int i=0;i<(sizeof arr);i++){
    

    Since arr is a pointer to an integer, sizeof arr is equivalent to sizeof (int *), which is apparently 4 (32-bits) on your platform. That’s clearly not what you want. You only pass fillArray a pointer to the first element of the array.

    If you need the number of elements in the array a pointer points to, you need to pass that information. The C and C++ languages provides no way to tell how many bytes a pointer points to given just the pointer.

    You do it correctly in arrPoint. Do it that way in fillArray.

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

Sidebar

Related Questions

I have an array full of random content item ids. I need to run
I have an array full of sub arrays. I need to break apart the
I have a array that is full of image objects, I need to be
I have an array full of patterns that I need matched. Any way to
I have an array within an array that contains settings. I need to figure
How do i explode this string '||25||34||73||94||116||128' i need to have a array like
I have an Array of Objects that need the duplicates removed/filtered. I was going
I have an array of filenames and need to sort that array by the
I have an array which i need to sort its elements by occurrence then
I have an array of shorts (short[]) that I need to write out to

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.