1

i want to use float values inside the array for example,

array[4];

array[0] = 3.544
array[1] = 5.544
array[2] = 6.544
array[3] = 6.544

float array[] (is giving me error)

but i dont know how to use help me, i am a c beginner

jleahy
  • 16,149
  • 6
  • 47
  • 66
Kishan
  • 115
  • 2
  • 2
  • 6
  • 3
    You are missign SOMETHING in your example. `float array[4];` should work just fine. Doing another declaration obviously won't. – Mats Petersson May 20 '13 at 10:41
  • 2
    Please post actual code. Also, please say what error you are getting. Finally, remove the C++ tag if your programming language is C. – Daniel Daranas May 20 '13 at 10:41
  • hey @Kishan! Please select the best answer and accept the answer. This is how stackoverflow works.! – Chirag Desai May 20 '13 at 10:44
  • possible duplicate of [How to initialize an array in C](http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c) – madth3 Oct 17 '13 at 04:43

4 Answers4

9

You have to specify the size if you are going to define array float in this way:

float array[4];

You can define the array without the size. but it should be in this way:

float array[] = {3.544, 5.544, 6.544, 6.544};

see the following topic for more details: How to initialize all members of an array to the same value?

Community
  • 1
  • 1
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • little note: put on the F at the end, they are floats {3.544F, 5.544F, 6.544F, 6.544F}; – Exceptyon May 20 '13 at 10:51
  • @Exceptyon, why?, there is no need to such thing – David Ranieri May 20 '13 at 11:02
  • 2
    @DavidRF: The result of `float x = 3.544;` is the decimal numeral “3.544” converted to `double` and then converted again to `float`. For certain rare numerals, this double conversion produces a different result than converting a numeral suffixed with “f” directly to `float`. In addition, there circumstances, other than initialization or direct assignment of a floating-point object, in which the use of `double` rather than `float` changes the semantics of C expressions. – Eric Postpischil May 20 '13 at 11:12
2
float array[4];

array[0] = 3.544;
array[1] = 5.544;
array[2] = 6.544;
array[3] = 6.544;

This should work buddy.

Chirag Desai
  • 1,249
  • 1
  • 10
  • 22
1

You can't create an array with no static size.

You can create an array like this on the stack, which is mostly when you have smaller arrays:

float myarray[12];

it is created in the scope and destroyed when that scope is left.

or you can create large arrays using malloc in C, the are allocated on the heap, these need to be manually destroyed, they live until you do so:

// create array dynamically in C
float* myheaparr = malloc(sizeof(float) * 12);

//do stuff with array


// free memory again.
free(myheaparr);
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
  • 1
    This answer is confusing. It presents two allocations in one set of source code. That is either pointless or is a failure to separate the examples and to state the purpose of each one. – Eric Postpischil May 20 '13 at 11:16
0

Specify number of elements you wanna work with while declaring.

float array[number of elements];

this will statically allocate memory for specified. Access each elements with index. eg

float array[4] will statically allocate memory for 4 floating point variables. array[0] refers to the first element and so on.

varuntv
  • 11
  • 2