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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:03:19+00:00 2026-05-27T12:03:19+00:00

I have a code in C with these lines included: int i; int *serie

  • 0

I have a code in C with these lines included:

int i;
int *serie = malloc(sizeof(int)); 

    for (i = 0; i <= 20; i++){
        serie=rand(); 
        printf("%d ",&serie[i]);/* *serie */
    }

It does work but I want to know why, with malloc I believe I am creating a dynamic array or pointer called serie so far my knowledge is:

& returns the address
* returns the content of the adress

With fixed arrays you use [] and with pointers ()

By testing &serie[i] seems to work but It does not *serie(i) or *serie[i] and *serie I think It does not too.

Can someone explain me these?

If I wanted to print the contents should not I put * instead of &, I think with dynamic arrays you use [] instead of () so it should be *serie[i] not &serie[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-05-27T12:03:20+00:00Added an answer on May 27, 2026 at 12:03 pm

    In this code, serie is a pointer to an integer. The line with malloc() allocates space for it, such that setting or getting an integer to/from *serie will work. The loop seems to incorrectly be setting the return value of rand() (an integer) to serie. That particular line should look like this, in your current code (but it’s not what you want anyway):

    *serie = rand();
    

    Because rand() returns an integer, and serie alone is a pointer to an integer. However, *serie is an integer that you can set to.

    In the printf(), you’re trying to access serie as an array, but that won’t work because you’ve only allocated a single element. Well, it will work, but only for element zero.

    If you’re trying to set and generate 20 random elements (but using a “dynamic” array), you might want something like this:

    int i;
    // note allocating the proper number of elements
    int *serie = malloc(sizeof(int) * 20);
    
    // note starting at and going < 20 for 20 elements
    for (i = 0; i < 20; i++) {
        serie[i] = rand();
        printf("%d ", serie[i]); // *(serie + i) will also work here
    }
    

    Note that any time you use square brackets to access an element, it’s dereferencing it, much like * does. So serie[i] and *(serie + i) are functionally equivalent.

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

Sidebar

Related Questions

So I have these lines of code: int maxY, maxX; getmaxyx(stdscr, &maxY, &maxX); It
I have the following code: #include <stdio.h> int main() { printf ("hello world\n"); return
So I have this code for these Constructors of the Weapon class: Weapon(const WeaponsDB
I have seen code around with these two styles , I am not not
I have already have this code to force these URLs to HTTPS: RewriteCond %{HTTPS}
I wrote this code I have these errors Cannot implicitly convert type x.Program.TreeNode' to
I have this code which will include template.php file from inside each of these
I have a code base, in which for Matrix class, these two definitions are
I have these two pieces of code, wich one is more readable? foreach decimal
I have a bunch of these little bits of HTML code repeated over and

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.