C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 26th, 2012, 12:19 PM
MasterFnarg MasterFnarg is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 MasterFnarg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 9 sec
Reputation Power: 0
Prototype for a function

This problem is really annoying me, so much so that I have signed up to the forum especially for it.

Basically what I want to do is create a function, it will have two arguments, one is a unsigned long, the other is a array of structs with a variable position within the array of unsigned long (one dimensional). This function will return zero.

So this is what I think it should look like,
void Function(MyStruct Array[unsigned long Variable], unsigned long Variable);

BUT I get the error "type name is not allowed" under "unsigned" inside the array and "expecting a ']'" under "Variable". This is really irritating and I have absolutely no idea what the proper syntax should be. Any help would be very much appreciated.

Reply With Quote
  #2  
Old November 27th, 2012, 07:25 PM
ediz ediz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 102 ediz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 35 m 33 sec
Reputation Power: 3
Like this

Code:

#include <iostream>

struct MyStruct
{
	int ID;
};

//This function takes a pointer to an array
int SearchA(MyStruct* array, int sID, const int SIZE);

//This function is exactly the same as the first one, but written different.
int SearchB(MyStruct array[], int sID, const int SIZE);

int main()
{
	const int SIZE = 5;
	MyStruct array[SIZE];

	//gives the array values
	for (int i=0;i<SIZE;i++)
	{
		array[i].ID = (5 + i * 2);
	}

	int indexA, indexB;

	indexA = SearchA(array,3,SIZE);
	indexB = SearchB(array,9,SIZE);
	
	
	return 0;
}

int SearchA( MyStruct* array, int sID, const int SIZE )
{
	//returns the index. returns -1 if not found.

	for (int i=0;i<SIZE;i++)
	{
		if(array[i].ID == sID)
		{
			return i;
		}
	}

	return -1;
}

int SearchB( MyStruct array[], int sID, const int SIZE )
{
	//returns the index. returns -1 if not found.

	for (int i=0;i<SIZE;i++)
	{
		if(array[i].ID == sID)
		{
			return i;
		}
	}

	return -1;
}


Comments on this post
MasterFnarg agrees: Helped me solve my issue. Now how the hell to rep?

Reply With Quote
  #3  
Old November 30th, 2012, 08:03 PM
MasterFnarg MasterFnarg is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 MasterFnarg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 9 sec
Reputation Power: 0
So either make it a pointer or leave the brackets open and put the value in later then? Wasn't sure the second was possible and was trying to avoid the first. Specification i'm working for isn't/wasn't entirely clear. Thanks for the help. Rep or whatever the equiv is if this forum has it/i can work it out.

Reply With Quote
  #4  
Old December 1st, 2012, 11:25 AM
ediz ediz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2012
Posts: 102 ediz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 35 m 33 sec
Reputation Power: 3
Arrays are pointers when you create one.

Writing it with brackets [] or * doesn't matter. They basically means the same thing (for arrays).

You know what a pointer is?

A pointer points on an address in memory. In this example i made, the variable array points to an MyStruct array in memory.

So when we send the array to the function, we actually send the pointer, not the whole array.

I recommend always using pointer in the function when working with arrays:

int SearchA(MyStruct* array, int sID, const int SIZE);


No need to rep.

Extra:

You know the difference between an static array and a dynamic array?

A static array, declared like this:
Code:
int arr[10];

or
Code:
const int SIZE = 5;
int arr[SIZE];

or
Code:
int array[] = {1,2,3,4,5};


* A static array cannot expand. If you define an static array to be the size 5, It will always be the size of 5.
* A static array requires a constant variable, when you declare the size.
* A static array is a pointer.

A dynamic array is declared like this:

Code:
int* arr = new int[10];


or

Code:
int size = 10;
int* arr = new int[size];


* A dynamic array can be expanded.

* A dynamic array is a pointer.

* Note that you don't require a constant to declare the size.

* You allocate memory to create a dynamic array. What you borrow, you have to give back, always. Other wise you get memory leaks.
To de-allocate memory, you simply write: delete [] arr; when you are finished with it.

* To expand a dynamic array, you simply write:

Code:
#include <iostream>

int main()
{
	int capacity = 10;
	int* arr = new int[capacity];

	//Fill the array with something
	for (int i = 0; i < capacity; i++)
	{
		arr[i] = 10+i;
	}

	//Now we say we want 10 more elements in the array, lets expand it

	//create a temp pointer
	int* temp;
	//Allocate it with the new capacity
	temp = new int[capacity*2];

	//Copy the elements to our new array
	for (int i = 0; i < capacity; i++)
	{
		temp[i] = arr[i];
	}

	//When we are done, deallocate our old arr, because its no longer needed.
	delete [] arr;
	//Point the arr pointer to our new array in the memory
	arr = temp;
	//arr now points on the same address as temp. We don't need temp anymore, lets point it to nothing
	temp = NULL;
	//increase the capacity
	capacity = capacity * 2;


        //Finished
        delete[]arr;

	return 0;
}

Last edited by ediz : December 1st, 2012 at 11:45 AM.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Prototype for a function


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap