C - Passing a single variable instead of an array [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm facing an issue on a PIC from Microchip but my question is more general. This will help me check if it is an issue from my code or from the debugger of MPLAB X which is sometimes buggy.
So here is my question, let's say I have this function:
int test( int *array )
{
int a = array[0];
return a;
}
Now, if I want to pass an array it's working, there is no issue. But let's say I want to use a single variable instead of the array like this:
int main()
{
while(1)
{
int test_variable_1 = 8;
int test_variable_2 = test( &test_variable_1 );
}
return 0;
}
I did not use an array but for me this should work as expected since the test_variable_1 is like an array of size of one int.
EDIT QUESTION: So, is it possible to pass a single variable like this (using the pointer with &) instead of a real array into an function? Also, is it a good/bad practice?
Thanks in advance and don't hesitate to tell me if I'm unclear.
Have a good day!
Adrien

So, is it possible to pass a single variable like this (using the pointer with &) instead of a real array into an function?
Yes. In fact C doesn't even know that you passed an array. For details, see Do pointers support "array style indexing"?
Also, is it a good/bad practice?
It's fine. Though ideally the function should take the number of items as one of the parameters passed.

Related

How to unit test this c function? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have a simple c function,
uint_8 tempM_main()
{
if(g_variable1 > g_variable2)
tempM = g_variable1;
else
tempM = g_variable2;
}
where g_variable1 and g_variable2 are the global variable.
How can i able to unit test this scenario?
equivalent class and boundary class need to apply for this?
I am using Tessy tool for unit testing.
It has undefined behavior if you use the return value, because it says it should return an uint_8 but does not return anything. Before it can be tested in a sensible way, this should be corrected. Either via changing the signature or returning a value.
Let's take the case where you have added a return statement. You're obviously using globals, so you can test it like this:
g_variable1 = <value1>
g_variable2 = <value2>
assert(tempM_main() == <value3>);
assert(tempM == <value4>);
If you change the signature to return void, just remove the first assert and replace it with only the function call.

How to add new element to const char ** array [closed]

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 1 year ago.
Improve this question
What's up?
I am very interested in the question of how to add a new element to my array
uint32 glfw_extension_count = 0;
const char** glfw_extensions;
glfw_extensions = glfwGetRequiredInstanceExtensions(&glfw_extension_count);
return glfw_extensions;
I just need add to 'glfw_extensions' VK_EXT_DEBUG_UTILS_EXTENSION_NAME
The (great!) documentation says:
Pointer lifetime
The returned array is allocated and freed by GLFW. You should not free it yourself. It is guaranteed to be valid only until the library is terminated.
So clearly you can't do the necessary re-allocation. So you must allocate a new array, copy the data in, and then append your additional entry. I would assume that you can safely copy the string pointers themselves, didn't see a lot of discussion about that in the documentation page so it might need further verification.

Shift a whole block in an array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I was wondering if there is a possibility to shift a whole block in an array. I intend to delete one item in an array of dynamic length, and after I deleted it, I want the whole block to shift to the right. So far I do this element, by element, but this is not efficient. So I was wondering if there is a better solution.
Important: the order of the elements needs to be kept the same.
Using memmove will probably be more efficient than doing an element by element copy as then you can take advantage of the fact you're doing a bulk move rather than lots of little moves (and compilers often provide highly optimized memmove implementations), but that's about it. You need to move everything around in memory, so you're going to have to move it.
If you're doing this a lot with your arrays, it probably means you need a different data structure.
You can use linked list instead of array. In linked list deleting is easy. You just have to point the next next elements.
Use a ring buffer, like this:
unsigned idx;
int Buf[1024];
void EnterValue(int value)
{
idx++;
Buf[idx & 1023] = value;
}
int GetOldValue(unsigned age)
{
assert(age<1024);
return Buf[ (idx-age) & 1023 ];
}

Accessing multidimensional arrays without using loop? [closed]

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 9 years ago.
Improve this question
Hi friends I was working on a project where we need to use quite a few multidimensional arrays. I am using for loops to access different array elements, then I just thought what if I don’t have a liberty to use looping? How am I going to access array element?
I am new to C, so thought of discussing here, I am sure there might be thousans of people who could have thought the same way, and hopefully found the solution.
Below example of multidimensional array is give, please guide me.
Thanks
static int t[3][2][4] = { {2,4,3,6,
1,6,7,9,},
{8,2,1,1,
2,3,7,3,},
{1,6,2,4,
0,7,9,5,},
};
Please Help me...thanks!
If you need to go through all of the values inside the loop without manual handling (i.e. x = t[1][1][1] then x = t[1][1][2] etc) then you want to use loops, enhanced loops or iterators. However since you're using C the only of those three options available are standard loops, which you're trying not to use. So... there's mo straight forward way to do that really.
If you're willing to use some other C libraries however then there may be more options for you. Iterator libraries probably exist.
A non-straightforward way to do it (if you're looking for one) could be through recursion, however that's really quite wasteful. I advise you just use loops :P
What are you trying to prove with loops and without loops should be first thought.
If u want to access all the elements and not use loop is like writing a lot of code manually and waste of memory(in your case 3 * 2 * 4 no of lines instead of few ).
Instead of showing the array if you had put in your code how and where you accessing elements it would have been more clear to tell what you wanted .

What is pf apply in c? [closed]

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
I have a function
void join(table first, void(*pfapply)(const char *inp1,const void *inp2,void *addn),const void *addn);
here first is a instance of structure table as below
struct table
{
char inp1;
int inp2;
}struct table *first;
Now how to add this extra variable "addn" in this "join function"
In my symbol table i have two values inp1 and inp2..how to add addn to my structure using the above function?
I need to bind addn to the two variables inp1 and inp2...
The addn is probably just the API being friendly and providing support for an additional parameter to the pfapply() callback.
This is a sign of good design, since it allows the callback to access some of its data without having to make it global.
If you don't need any additional data, just ignore the argument inside your apply-function, and call join() with NULL as the third argument.
You don't need to change anything in the declaration of table, all is fine.

Resources