pointers as indicis for dynamic arrays

for this program i am required to use a pointer for every variable i declare. I have only one problem with this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int getlargest(int &list, int count)
//this function goes through the data stored in the dynamic array
//finds the largest number in the array
//and returns that number to printstuff(int,int)
{
	int *num;
	num = new int;
	*num = 0;
	for(int *i=0;*i<count;*i++)
	{
		if (*i[&list]>*num)
			*num = *i[&list];
	}
	return *num;
}


it isn't working when i try to use it as an index of an array... I had originally used i as an int but my instructor was not pleased and I'm not sure how to make this work. the error i get says "illegal index indirection not allowed." not sure what this means... I can't find any literature on using a pointer as an index for an array so any help would be appreciated
Last edited on
for(int* i = new int(0); *i < count; ++*i)

Though that's not your only problem. In your code you treat i as a (pointer to) array while it should be a (pointer to) the index.
Judging by your names list should be an array, but it is in fact only a singe integer. Did you mean int* list instead?
Last edited on
I passed the address of the array using the ampersand... When i was using just int i it was working just fine... would not int* list pass in a value or does it not work that way because its an array?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int getlargest(int *list, int count)
//this function goes through the data stored in the dynamic array
//finds the largest number in the array
//and returns that number to printstuff(int,int)
{
	int *num;
	num = new int;
	*num = 0;
	for(int *i= new int(0);*i<count;*i++)
	{
		if (*list[*i]>*num)
			*num = *list[*i];
	}
	return *num;
}

i tried this but i get illegal indirection errors...
i tried this too to the same results...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int getlargest(int *list, int count)
//this function goes through the data stored in the dynamic array
//finds the largest number in the array
//and returns that number to printstuff(int,int)
{
	int *num;
	num = new int;
	*num = 0;
	for(int *i= new int(0);*i<count;*i++)
	{
		if (*i[&list]>*num)
			*num = *i[&list];
	}
	return *num;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int getlargest(int &list, int count, int i)
//this function goes through the data stored in the dynamic array
//finds the largest number in the array
//and returns that number to printstuff(int,int)
{
	int *num;
	num = new int;
	*num = 0;
	for(i=0;i<count;i++)
	{
		if (i[&list]>*num)
			*num = i[&list];
	}
	return *num;
}

what i did was declare int* i in the main and pass the value it pointed at to all functions that used the counter... maybe its cheating but it's declared as a pointer and it does it's job... actually saves the program from having to allocate and delete hundreds of counter pointers so i am armed with my argument if he dings me for it! Thanks for the help
does the code work?
yeah it works i think i'm cheating by passing the value at the address though oh well...
Topic archived. No new replies allowed.