Any append (Python) or push_back (C++) equivalent in C? [duplicate] - arrays

This question already has an answer here:
Python-like array filling - C equivalent
(1 answer)
Closed 2 years ago.
I'm looking for an "append" (Python) or "push_back" (C++) equivalent in C to fill an array of strings (char). Does it exists?

The question doesn't make sense because you cannot add elements to an array in C. In C, you set the size of the array when you create it, and the size cannot change.
If you want to "append", you have to create a big array (e.g. 1000 items), and use a separate variable to remember how many items you're actually using (the rest are spare).

Related

if at least one element of array in Perl [duplicate]

This question already has answers here:
How can I check if a Perl array contains a particular value?
(12 answers)
Closed 7 years ago.
Maybe my question is really easy, but I would like to know the best way and efficient one.
Let's we have an array of strings and we want to compare it with another string.
say,
my #array = {"hi","bye","you","shadow", "hi"}
Now I want to check if at least one element of the array equals hi then there is some condition.
May I know your idea about it. I know that within a for loop one could do it easily, but Would you would suggest as a good one?
Something like
my #array = qw (hi bye you shadow hi);
my $hi_count = scalar(grep {$_ eq 'hi'} #array);
print $hi_count;
This will print 2 as there are two words that equal hi.

Reading in to a character array of unknown size [duplicate]

This question already has answers here:
C dynamically growing array
(10 answers)
Closed 9 years ago.
How do I read in a c-string in to a character array without knowing the size of the string that the user will enter ?
Without any code or further description of your issue it's hard to know what you're trying to achieve, but, one of the following might be appropriate for your needs:
Use a preallocated array of some maximum size you know is greater than the number of characters that will be entered.
Create an empty std::string, and then use the string "+=" operator for each character entered. Then you can convert back to an array using the c_str() method.

How to swap two variables in one line in C? [duplicate]

This question already has answers here:
Interchanging values of two variables by xor operator using references or pointers
(2 answers)
Swapping two variable value without using third variable
(31 answers)
Closed 9 years ago.
I want to know that is there any other way of swapping 2 numbers in one line and of course without 3rd variable.
I know one way of doing this:
b=a+b-(a=b)
or
a=a+b-(b=a)
both are same(approximately). If you know then please help me out.
The frequently cited classic answer that you are probably looking for is:
a^=b^=a^=b;
But, it is technically wrong, because it changes the same variable more than once before a sequence point.
Use bit twiddling in C. Following swap two variables:
if (a != b) {
a ^= b ^= a ^= b;
}

What is the history of using i in for loops? [duplicate]

This question already has answers here:
Why are variables "i" and "j" used for counters?
(23 answers)
Closed 10 years ago.
In all the programming languages I have come across there seems to be the best practice to use variable i in for loop iterations. Usually i is followed by l in the nested loop. This seem to apply both for statically compiled and scripting languages.
What is the history of this practice? Does i mark for integer, index, or something else? Why, for example, we don't use x which would be more common, considering math background.
I've got two theories: i can stand for 1) index 2) integer (value of integer type)
It makes most sense that i stands for index because the loop is over each element of an array and each element is indexed.

Concept of function pointers in C? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What is the point of function pointers?
hi all,
I want to get the basic and concrete idea of function pointers in C language.
ie 1) its usage in C
2) main applications it is currently using
3) unique features
4) its scope in embedded applciations etc
Hoping your co operation in this too.
__Kanu
Function Pointers are pointers, that is variables, which point to the address of a function.
Nice example here. Also this answer is a must read.

Resources