Giving an array in form of pointer to a function as parameter

Hello guys,

I need to give an array to a function. Different functions will give this function arrays of different sizes, so i decided to take a pointer.
Here is what i have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  void funcA()
{
String type[3];
type[0]=„test1“;
...
 // Fill array complete with stringd
}

Void funcB(String* type)
{
String temp = „mystring“;
Int size = sizeof(type)/sizeof(String);
For(int i=0; i<size;i++)
{

If(temp == type[i])
{
 //Do whatever
}
}
}




My problem is that my size of the type array in fjncb is always 1 st makes thing something is wrong in the way i am trying to give the array to the gunction.

Appreciate any help
Hello RKirsch,

Welcome to the forum.

Based on the code you posted the first thing I see is in "funcA". You define an array of strings and fill the array, but this array has local scope to the function. When the function ends so does the array and anything that you have filled it with. Trying to pass this array to another function from anywhere other than "funcA" would mean that you have nothing to point to.

In "funcB":

Line 9. "String" is the wrong way to refer to the type. It is either "std::string" or "string" all lower case letters.

Line 11. Not sure what is around the string, but it will not work. Whatever that character is it is not the double quote that is needed.

Line 12. Best not to use a C function on a C++ string. Also this use of sizeof is written wrong. Using the string class you have access to ember functions ".size()" and ".length()" which will tell you how many characters are in the string. I think what you may want here is std::size_t size = type[index].size;. Where "std::size_t" is another name for unsigned int which is what the .size()" function returns.

If the point is to determine how many elements in the array it is better to pass a variable for the size of the array to the function. And there is an easy way to do that. Since you have only posted part of your code it will take me a bit to work up an example.

Hope that helps,

Andy
Hello RKirsch,

This is an idea of what you can do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>

constexpr std::size_t MAXSIZE{ 3 };

void funcA(std::string* type)
{
	for (size_t lc = 0; lc < MAXSIZE; lc++)
	{
		type[lc] = "test" + std::to_string(lc + 1);
	}
	type[1] = "mystring";
	//...
		// Fill array complete with stringd
}

void funcB(std::string* type)
{
	std::string temp = "mystring";

	for (int i = 0; i < MAXSIZE; i++)
	{

		if(temp == type[i])
		{
			std::cout << "\n Do whatever.\n";
			//Do whatever
		}
	}
}

int main()
{
	std::string type[MAXSIZE];

	funcA(type);

	funcB(type);
	
	return 0;
}  //  End main 


Just so you know.

1
2
3
funcA(std::string* type)

funcA(std::string type[])

Both styles are the same. The second example will degrade to a pointer if used. Either way both access the array in the same way with a subscript. And I do not believe the is any advantage in one or the other.

Hope that helps,

Andy
Line 12. Best not to use a C function on a C++ string. Also this use of sizeof is written wrong. Using the string class you have access to ember functions ".size()" and ".length()" which will tell you how many characters are in the string. I think what you may want here is std::size_t size = type[index].size;. Where "std::size_t" is another name for unsigned int which is what the .size()" function returns.

Yes the use of sizeof is incorrect in this content, but remember the type is a pointer to a String (whatever String is). A pointer has a specific implementation defined size and that is what sizeof will return for "type" in this instance.

Also size_t is an implementation defined unsigned type, usually either unsigned long or unsigned int (at the present time for most desktop implementations). However it could be any unsigned type.
@ jlb,

Also size_t is an implementation defined unsigned type, usually either unsigned long or unsigned int (at the present time for most desktop implementations). However it could be any unsigned type.

Thank you. It looks like somewhere I came to the wrong idea.

Andy
Topic archived. No new replies allowed.