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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:36:26+00:00 2026-05-28T03:36:26+00:00

I am new to C, and this struct, representing an image, is confusing to

  • 0

I am new to C, and this struct, representing an image, is confusing to me. It’s used in this Graphics Gem.

Can someone explain the proper instantiation and use of the struct, especially with regard to the scanline pointer array?

typedef unsigned char Pixel;

typedef struct {
    short Hres;  /* no. pixels in x direction */
    short Vres;  /* no. pixels in y direction */
    int   Size;  /* size in bytes */
    Pixel *i;    /* pixel array */
    Pixel *p[1]; /* scanline pointer array; position (x,y) given by image->p[y][x] */
}   Image;

Also: is the point to avoid the multiplication implicit when indexing a 2D array? Should it not then be **p which can be allocated to Vres * sizeof(size_t) and populated with the appropriate row pointers?

Update

I think I understand. Is this example block valid?

int m, n, y, x; /* Vres, Hres, index variables */
Image *image;    
image = malloc(sizeof(Image) + (m - 1) * sizeof(Pixel*));
image->Hres = n;
image->Vres = m;
image->Size = m*n*sizeof(Pixel);
image->i = malloc(image->Size);

for (y=0; y<m; y++)
{
    image->p[y] = image->i + (y * n);
    for (x=0; x<n; x++)
    {
        image->p[y][x] = 0; /* or imageSource[y][x] */
    }
}
/* use image */
free(image->i);
free(image);

Finally, on modern computers (with lots of memory), does it make sense to use such a scanline pointer array rather than a 2D array? In this case the only difference would be the implicit pointer multiplication.

  • 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-28T03:36:27+00:00Added an answer on May 28, 2026 at 3:36 am

    The last member, p, of the structure Image is used as a C89 flexible array member.

    Flexible array member is a C99 feature and before C99, people sometimes used what was called the struct hack to achieve a similar behavior with C89.

    Here is how to dynamically allocate a Image structure object with only one array element:

    Image *bla1, *bla2;
    bla1 = malloc(sizeof *bla1);
    

    and here is how to allocate a structure object with n array elements:

    bla2 = malloc(sizeof *bla2 + (n - 1) * sizeof bla2->p[0]);
    

    After correct initialization of the Pixel * pointers in the p array, you can access the Pixel values like this:

    bla2->p[x][y]
    

    Regarding the conformity of the struct hack, C99 Rationale says that

    the validity of this construct has always been questionable.

    while a C Defect Report (DR #051) says that

    The idiom, while common, is not strictly conforming.

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

Sidebar

Related Questions

They can be defined like this Struct.new(:x, :y) But what can usefully be done
I am new to this. I want to design an image gallery in my
In our code we used to have something like this: *(controller->bigstruct) = ( struct
I'm not sure how to explain this but this piece of code bellow can
I am new this sharepoint development and i have task in hand to do
New to this library (no more familiar with BeautifulSoup either, sadly), trying to do
Why does this: (new[]{1,2,3}).Cast<decimal>(); result in an InvalidCastException: Specified cast is not valid.
Im new on this project and am going to write, what i thought was
I'm new to this SCM, but since SVN is gaining popularity I was going
I'm very new at this, but I need to create new tables from existing

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.