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 8628077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:26:40+00:00 2026-06-12T08:26:40+00:00

Possible Duplicate: How to get the size of dynamically allocated 2d array I can’t

  • 0

Possible Duplicate:
How to get the size of dynamically allocated 2d array

I can’t find what I did wrong here. I want to create an array which its size is based on user input, get the data (integers) for the array from user input, and print the array integers.

The problem is that it only prints the first array element, i.e intArr[0].

int main()
{
    int i, n, *intArr;

    printf("Type the array size:\t");
    scanf("%d", &n);

    intArr = (int *)malloc(n * sizeof(int));

    for (i = 0; i < n; ++i)
    {
        printf("Type a number\t");
        scanf("%d", intArr + i);
    }

    printArr(intArr);
}

void printArr(int *arr)
{
    int i; 
    for (i = 0; i < (sizeof(arr) / sizeof(*arr)); ++i)
        printf("%d ", *(arr + i));
}
  • 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-12T08:26:41+00:00Added an answer on June 12, 2026 at 8:26 am

    The type of arr is an int* so sizeof(arr) will be the sizeof(int*) and not the number of elements in arr. On your system sizeof(int*) and the sizeof(int) is the same, giving 1 as the result so the loop prints one element only.

    Pass the number of elements as an argument to the printArr() function.

    Note:

    • free() what you have malloc()d.
    • casting return value of malloc() is unnecessary.
    • check return value of scanf() to ensure successful.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Use templates to get an array's size and end address Can someone
Possible Duplicate: Get total size of folders with find & du I can use
Possible Duplicate: How to get screen size and respond to it? Can I get
Possible Duplicate: Auto-size dynamic text to fill fixed size container How can I get
Possible Duplicates: How can I get the size of an array from a pointer
Possible Duplicate: PHP get all arguments as array? Well, In java I can do
Possible Duplicate: Get full path of a file with FileUpload Control I want to
Possible Duplicate: get name of current PHP script in include file Can an included
Possible Duplicate: Data transfer from JavaScript to PHP. How can i get the browser's
Possible Duplicate: Javascript window resize event I want to get an event which is

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.