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

The Archive Base Latest Questions

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

I made a typedef for float arrays, like this: typedef float fooType[]; So that

  • 0

I made a typedef for float arrays, like this:

typedef float fooType[];

So that I can declare and initialise float arrays statically like this:

fooType myArray = {1.0, 2.0, 3.0}

That works fine. I can say myArray[2] with no problems and generally use my fooType as I’d like. In the context of my program it’s very clear that fooType is an array type (it’s actually a giant lookup table) which is why I’m not simply doing typedef float fooType.

How do I then declare and use a variable to point to bar and access the elements of bar? I cannot declare a simple variable like this:

fooType bar = myArray;

Since that errors with invalid initializer; I didn’t expect it to work since the compiler doesn’t know how much memory to allocate. I tried a few things with pointers:

fooType *bar = myArray; 

This gives a warning: initialization from incompatible pointer type and errors when I access the elements with bar[1].. This declares fine, as I’d expect it to since the types match now:

fooType *bar = &myArray;

But as above the compiler errors with an invalid use of array with unspecified bounds when I say bar[1].

This works:

float *bar = myArray; 
float val = bar[3]; 

But I don’t want to because I lose the readability of my typedef.

I’ve been buried for a long time in a cuddly OO world (Java), so I hope to understand from this what’s idiomatic in C in this situation. I’m quite prepared to change my use of the typedef to suit convention if there is no direct solution. I have a feeling there is though, and it’s not complicated.

Thanks

  • 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-27T02:19:54+00:00Added an answer on May 27, 2026 at 2:19 am

    The problem you’re encountering here is the fact that arrays are not pointers in C, but rather decay to pointers to the first element when used on the right-hand side of the assignment operator. Thus, if you declare an array like

    fooType myArray = {1.0, 2.0, 3.0}
    

    the type of myArray is float[3], and &myArray is (*float)[3], or “pointer to an array of 3 floats”. Thus you can’t simply say

    fooType *bar = myArray;
    

    The type fooType* is not a pointer to a float … thus it’s an incompatible pointer type. Doing

    fooType* bar = &myArray;
    

    does work, but the problem you’re encountering is that bar[1] is not an element in the array myArray, since bar itself is a pointer. You would have to dereference bar like (*bar)[1] to access an element in myArray. For instance:

    typedef float fooType[];
    fooType myArray = {1.0, 2.0, 3.0}
    fooType* bar = &myArray;
    (*bar)[1] = 5.0;
    

    Finally, keep in mind that with C, you cannot assign one array to another array … so you can’d do something like:

    fooType bar = myArray;
    

    This goes back to the whole issue of arrays and their decay into pointers to the first element, and the fact that the type of the array itself is not the same as the type of the first element in the array.

    You may in the end simply want to typedef the array, and then typedef the array element types since of using float so that anyone using your code will know what one type of element belongs with the other.

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

Sidebar

Related Questions

Made this nice little loop for hiding and showing div's, works as a charm
To save some space in my code, I made this typedef: typedef clients.members.at(selectedTab) currentMember;
Both this style: struct _something { ... }; typedef struct _something someting; and that
if I make a typedef someobject* pntr ; I can use this typedef to
I have made a dll that compiles fine in 32bit mode, but when compiling
This code works if the 7th line down says typedef char ItemType but instead
I made a class that derives from Component: public class MyComponent: System.ComponentModel.Component { }
I made a code that translate strings to match each word from the array
I made a UIView using Interface Builder with a top bar that has Cancel
I am stuck with a design problem that I hope you guys can help

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.