-2

I want to be able to take input with a function but I want to store the information. Is there anyway to declare a variable that doesn't have a name and only can be accessed by a pointer?

something like this:

float* = float NULL 5;

Thanks.

EDITED: What I needed to do was to have an infinite amount of arrays that store a very large amount of ints. However, I am loading all this data from files and I need to access it from many other classes. I know how many times the function is run so i can keep them in an array but I don't know how many ints are or potentially are loaded

So the pointers son't need to have names because they will be stored in an array to be used later. I just wanted to be able to create global variables multiple times and just access them from an array.

BlueSpud
  • 1,555
  • 3
  • 19
  • 44

8 Answers8

2

The answer is NO. You have to have memory allocated for the variable.

Either the function (that returns value) should allocate memory, or your caller function.

But what you are trying to do is not even fitting C syntax.

anishsane
  • 20,270
  • 5
  • 40
  • 73
0

Well, you can allocate new memory from the heap with malloc(), and store stuff in there. That is probably what you want, the function can then return the pointer and hand it over to other functions. When the data is no longer needed, it should be de-allocated with free().

unwind
  • 391,730
  • 64
  • 469
  • 606
0

The simple answer is NO! So why do you want to do this daft thing?

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • What I needed to do was to have an infinite amount of arrays that store a very large amount of ints. However, I am loading all this data from files and I need to access it from many other classes. I know how many times the function is run so i can keep them in an array but I don't know how many ints are or potentially are loaded – – BlueSpud Nov 06 '12 at 13:32
0

No. You could invent a name that you're not using otherwise:

 float myFloatINTERNAL_DONTUSE;
 float * const myFloat = &myFloatINTERNAL_DONTUSE;

but what for? Just declare an ordinary local variable and use the & operator when you need its address.

hmakholm left over Monica
  • 23,074
  • 3
  • 51
  • 73
0

Yes you can.

float* p = malloc(sizeof(*p)); 

You nave a named pointer variable p, but the object it points to has no name. Don't forget to free the memory later.

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
0

No, C standart (so obviously your compiler too) doesn't support a variables without names. When you declare a pointer om something, you must give it a name too.
Well, what you want to do there, reasonably, is to create a variable in heap and then access it by a pointer with name:

float * const ptr = malloc(sizeof(float));

That's it, so here you go.

PaulD
  • 212
  • 2
  • 16
0

Suppose, there had been a way, to do it,

How would you access the variable without using a name?

Even pointer is a "named pointer variable" :-)

anishsane
  • 20,270
  • 5
  • 40
  • 73
  • What I needed to do was to have an infinite amount of arrays that store a very large amount of ints. However, I am loading all this data from files and I need to access it from many other classes. I know how many times the function is run so i can keep them in an array but I don't know how many ints are or potentially are loaded – BlueSpud Nov 06 '12 at 13:32
  • Then create a linked list? We can later optimize in terms of memory in case this is the desired solution. (linked list need additional memory for next pointer & would be costly, if data is 4 bytes int & next is also 4 byte ptr) – anishsane Nov 06 '12 at 13:35
  • Yes. load a bunch of files. put them all in an array and do something to all of them. get rid of them. – BlueSpud Nov 06 '12 at 13:40
0

Besides dynamically allocated objects (created using malloc) there are also supported from C99 compound literals.

E.g. construction like below

char **foo = (char *[]) { "x", "y", "z" };

or for your purposes

float *ptr = (float[]){5};

construct an array and actually you have only pointer to it without having object name itself.

codewarrior
  • 1,269
  • 1
  • 9
  • 14