C++ Passing an Array Pointer as a Function Argument

C pass int array pointer as parameter into a function

In your new code,

int func(int *B){
*B[0] = 5;
}

B is a pointer to int, thus B[0] is an int, and you can't dereference an int. Just remove the *,

int func(int *B){
B[0] = 5;
}

and it works.

In the initialisation

int B[10] = {NULL};

you are initialising anint with a void* (NULL). Since there is a valid conversion from void* to int, that works, but it is not quite kosher, because the conversion is implementation defined, and usually indicates a mistake by the programmer, hence the compiler warns about it.

int B[10] = {0};

is the proper way to 0-initialise an int[10].

Passing an array pointer as a parameter

Is the array passing to the function from main without correct?

Yes passing the array itself is correct, but you assign the result of the function to item number 10 here: receive[9] = .... Remember that arrays in C use 0-indexing, so this is out of bonds. You don't really need to return anything from the function in this case. Consider changing it to:

void reaArr (uint8_t len, uint8_t *data);

Also is my way of developing the function and passing the array as a pointer a good code strategy?

Generally, yes, caller allocation is preferred since that keeps allocation and algorithm separated. Had this example been a real I2C driver then you'd be reading from hardware buffers inside the function, instead of from a local array.

Passing an array as a parameter in C

This function declaration

void func(int v[]){
v[0] = 1;
}

is adjusted by the compiler to the declaration

void func(int *v){
v[0] = 1;
}

From the C Standard (6.7.6.3 Function declarators (including prototypes))

7 A declaration of a parameter as ‘‘array of type’’ shall be
adjusted to ‘‘qualified pointer to type’’, where the type qualifiers
(if any) are those specified within the [ and ] of the array type
derivation.
If the keyword static also appears within the [ and ] of
the array type derivation, then for each call to the function, the
value of the corresponding actual argument shall provide access to the
first element of an array with at least as many elements as specified
by the size expression

On the other hand, in this call

func(v);

the array designator v is implicitly converted to a pointer to its first element.

The C Standard (6.3.2.1 Lvalues, arrays, and function designators)

3 Except when it is the operand of the sizeof operator or the unary &
operator, or is a string literal used to initialize an array, an
expression that has type ‘‘array of type’’ is converted to an
expression with type ‘‘pointer to type’’ that points to the initial
element of the array object and is not an lvalue. If the array object
has register storage class, the behavior is undefined.

That is the function call is equivalent to

func( &v[0] );

So in fact the first element of the array is passed to the function by reference through a pointer to it. Dereferencing the pointer by means of the subscript operator (the expression v[0] is equivalent to the expression *v)

v[0] = 1;

the referenced first element of the array is changed.

Passing an Array as Argument to Function in C

char request[] is not the same as char *request[MAXSTRING]. The former declares an array of characters (i.e. a string), the latter an array of pointers to char, i.e. an array of strings.

So declare it correctly in your function:

int findMatches (const char *filename, char *request[]) {

Next you will need a way to detect the end of the array of strings contained in request. Either pass a count to findMatches() or arrange for the last string to be NULL. If using a count you can redefine the function to accept a count:

void findMatches (const char *filename, char *request[], int n) {
for (int i = 0; i < n; i++) {
printf("%s \n", request[i]);
}
}

And call it like this:

findMatches("filename.txt", request, agrc-1);

Also, the use of MAXSTRING in char *request[MAXSTRING] seems confused. You seem to want an array of strings, but MAXSTRING seems to be a maximum length of a string. It's unlikely that you will have 1000 arguments to your program.

C++ passing an array pointer as a function argument

You're over-complicating it - it just needs to be:

void generateArray(int *a, int si)
{
for (int j = 0; j < si; j++)
a[j] = rand() % 9;
}

int main()
{
const int size=5;
int a[size];

generateArray(a, size);

return 0;
}

When you pass an array as a parameter to a function it decays to a pointer to the first element of the array. So there is normally never a need to pass a pointer to an array.

Using a pointer to an array passed as a parameter of a function

You can pass the ipv4 int array to a pointer to int argument, what will happen is that it will decay to a pointer to its first element, to work properly with the array inside the function you should also pass the size of the array.

For example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int validate_ip(char *ip, int *ptid, size_t size) {

const char delim[] = ".";
char *token;
size_t i = 0;

//tokenize the ip and convert it to int
//this is for sample purposes, octet validation should be performed
token = strtok(ip, delim);
while (token != NULL && i < size) {
ptid[i++] = atoi(token);
token = strtok(NULL, delim);
}
return 1;
}

int main()
{
char ip[] = {"192.168.0.3"}; //sample ip
int ipv4[4];

if(validate_ip(ip, ipv4, 4)){
for (int i = 0; i < 4; i++)
{
printf("%d ", ipv4[i]);
}
}
}

This will need the validations to check if the octets in the ip char array are correct, but I believe you already done that.

One last note regarding my sample code, strtok will make changes in the ip char array, if you want to prevent this, make a copy of it and pass it to the function instead of the original ip.



Related Topics



Leave a reply



Submit