Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Is it possible to create a C-function that creates automatically a given number of variables? How are variables named?
Variables are an artifact of your source code. During runtime (which is when your function actually executes) there is only memory and registers. Maybe you want an array of a certain length?
The solution is to use array.
example:
//n is number of variables
int *var;
var= malloc(sizeof(int) * n);
variables are named var[0], var[1]....var[n-1]
If by "variables" you mean "global variables outside the scope of the function", and by "create" you mean "declare and define", then NO.
Do you mean like the register_globals 'feature' in PHP? Thank goodness, no.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Why do we use pointers in C Programming?
In general pointers are able to access the address where the int/float/char etc... is stored.
Are there any other uses?
It depends on what you try to achieve:
you can change the value of a variable inside a function
you can pass a struct to a function without having to copy all its fields - think of a function that receives a struct.
you can point to a specific variable/struct and point to it from other structs
and many other advantages (advantages is purpose dependant and it depends on whats your program is doing).
Pointers are quite basic C and there is a lot of material online you should get yourself familiar with them and the advantages will pop up themselves.
The reason is that pointers are used to bodge into C some vital features which are missing from the original language: arrays, strings, & writeable function parameters. They can also be used to optimize a program to run faster or use less memory that it would otherwise. A few tasks these days, such as programming microcontrollers, still need this.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to set and get the value of environment variable in Linux using C. Can anyone tell me how to use the setenv environment variable.
I want the environment value to be set to zero, and while using the getenv that environment value should be 0?
Try to use putenv() instead of setenv(). A little substrac of the manpage says:
"The setenv() function inserts or resets the environment variable name in the current environment list. If the variable name does not exist in the list, it is inserted with the given value. If the variable does exist, the argument overwrite is tested; if overwrite is zero, the variable is not reset, otherwise it is reset to the given value."
A question, when you said zero, you are talking about zero character right?. You have to put a string there.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm new to C and I have an assignment where I have to build a dictionary (Linked List in a way). Basically the user inputs several words,year and their definition this:
Example:
love_#_2004_#_LOVING
trade_#_2001_#_INVEST
etc...
And basically I need a function to scan the definition (Ex: INVEST)
and gives me the word trade.
If the definition is related to more than only one word to give me back all the words it relates to.
What sort of a function do I need to scan these strings?
If the word you search is always the last one and the formatting is always the same, then use strtok with _ and copy the last entry, which holds the string you are looking for.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Right now I'm trying to write a program, and one part of the it involves expanding an existing array by copying values from a previous array into an array with a larger size.
The way I'm doing this by using an int variable which is defined in a previous point int the program by user input.
int[x] array;
int[x + 1] array2;
Will this work, or do I have to initialize a separate int variable with value of x + 1?
The correct syntax is:
int array[x];
int array2[x+1];
C 1999 and later supports this (with the value of x determined at run time), although it is optional in C 2011. Some compilers (of questionable quality) do not support it.
The space available for objects of this sort is typically limited to one to eight mebibytes or so, and that space must also serve for other program needs, so it should be used only for small arrays.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I found this line in Linux Audio drivers soc-core.c inside sound folder:
int regsize = codec->driver->reg_word_size * 2;
Can anybody please explain the meaning of * 2?
Multiply the contents of codec->driver->reg_word_size by 2. I guess this is a translation between size in words to size in bytes.
Multiplies that value by 2. That's all it does
Well, I can just guess, but it looks like this:
codec is a pointer to a structure, which has a pointer to another structure in driver, which has a member variable reg_word_size (which it seems is, like the name says, the size of a register word). This value gets doubled (*2).
This could be, like the other answer says, a conversion between bytes and words. However, it could probably also just mean that this regsize should be twice as big as the reg_word_size.