Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Setts

macrumors newbie
Original poster
Jul 8, 2010
2
0
I am in the process of developing my 1st iPhone App. I am doing well just from the documentation available from Apple but I can't seem to get by my latest roadblock. I pass an argument to a method and want it to return some calculated related data in a "struct"ure. I have used typedef and struct to define this data in the method. I have used NSLog to verify that the data elements are indeed populated correctly. I would like to have a pointer to the structure returned by the method. How do I do that and then relate the same struct in the calling (messaging?) method?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
The definition of your struct and typedef will need to be in a .h file that is included anywhere that this method can be used. From there, you'll need to declare a pointer to this type of struct in your method, then allocate memory the size of your structure and assign the pointer to this memory to the pointer you declared. You can then fill in the struct and return the value of the pointer. It would go something like this:
Code:
#include <stdio.h>
#include <stdlib.h>

struct myInfo {
	int doubled;
	int tripled;
	double split;
	int squared;
};

typedef struct myInfo myInfoType;

myInfoType *infoOfNum(int);

int main(int argc, char *argv) {
  myInfoType *infoOne = NULL;
  infoOne = infoOfNum(17);
  printf("Double: %d\tTriple: %d\tSplit: %f\tSquared: %d\n",infoOne->doubled,infoOne->tripled,infoOne->split,infoOne->squared);
  free(infoOne);
}

myInfoType *infoOfNum(int x){
  myInfoType *localInfo = NULL;
  localInfo = malloc(sizeof(myInfoType));
  if(!localInfo) {
    fprintf(stderr,"Could not allocate memory");
    exit(-1);
  }
  localInfo->doubled = x*2;
  localInfo->tripled = x*3;
  localInfo->split = x/2.;
  localInfo->squared = x*x;
  return localInfo;
}

This is plain C, but it should be easy to adapt. The real problem with code like this is that infoOfNum returns a pointer to memory that needs to be free'd. This is no fun, in most cases. It would probably better to accept a pointer to this struct type and fill it in in the function. That way the caller can pass the address to a local struct, or allocate themselves knowing that they must free it themselves later. The only reason i wrote it this way was because you said you wanted to return a pointer to the struct.

-Lee

EDIT: Any reason your not just using a class with instance variables to communicate this information? Structs have their place, but memory management will be much easier if you use an object for this.
 

Setts

macrumors newbie
Original poster
Jul 8, 2010
2
0
Lee: Thanks for the quick input. I'll review your example but I think your right that I should pass a pointer to a struct in the calling method. Thanks again. I'll let you know how I fare. John
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.