Hi.

I'm newbie in C language (and in programming in general) and I don't know if this is possible.

Imagine you have an integer variable called "number" with a number, and when you executes a function, this function creates a variable called "audio1" and increments number (number ++; ). The next time this function will create another variable called "audio2".

Is this possible?

Thanks.

Recommended Answers

All 7 Replies

If I understand you correctly, you want to create variables dynamically at run time.

No. It is not possible.
C is strongly typed. Variables need to be declared before hand.

What are you trying to do? Variable names are largely irrelevant; they're just a means of accessing memory in your system. Learn about pointers and you'll understand how this works.

What you are attempting to do can be done with arrays.

Imagine you have an integer variable called "number" with a number, and when you executes a function, this function creates a variable called "audio1" and increments number (number ++; ). The next time this function will create another variable called "audio2".

With audio as an array, you can reference audio[1], audio[2], etc.

A name in C is a statically (before execution) defined program artifact, syntacticaly it's an identifier.

There are dynamically (in run-time) created and destroyed data objects and statically (before execution) defined data objects (literal constants). The program can't modify constants (literals).

A variable is a named data object. In other words, it's a data object with (statically defined, see #1 above) name.

Therefore you can't create new variables in run-time because you can't create new names in run-time. All names are defined in your program before execution (in actual fact before compilation).

However you can create new (anonymous) data objects (via malloc library function) and work with them via pointer variables.

Hi. First, thanks to all and sorry for the time i take to response.

What i'm trying to do is an audio program, in wich i want to have a button that call a function (this is what i have) and this function adds a new audio input, which it should have a name or something so i can identify. I wanna have the audio inputs i want, add and remove it.

If you want more info this is my project page and this is the code i'm using.

I wanna the function jack_add_input() in the line 56 to add an input when i execute the function.

I can rewrite all the code, since i'm newbie and i have no much idea.

Thanks.

So in your calling function, create an array with an arbitrary size large enough to hold all the inputs your program will ever use: TypeAudioInput audioinputs[200]; Then you can pass an individual element to the function through a pointer: jack_add_input(&audioinputs[i]); Keep in mind that jack_add_input will have to take a TypeAudioInput* as a parameter (TypeAudioInput is whatever type Jack uses to represent audio inputs).

Or you can get it through the return value: audioinputs[i] = jack_add_input();

Hi.

Tank you, really.

This makes me think again on how to do my project, but since my project is intended to learn some of programming and to waste time without boring, it takes no importance.

Thank you again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.