5

I want to know how to pass structures to another function and subsequently access that structure in the called function. I'm developing for the iPhone and the reason I'm using structs is so that I can eventually pass data as structs to a server being built in C.

Here's the structure:

struct userInfo{
    NSString *firstName;
    NSString *lastName;
    NSString *username;
    NSString *email;
    NSString *ipAddress;
    double latitude;
    double longitude;
};

Here I'm simply fetching some user inputed data along with some CoreLocation data and the iPhone's IP Address:

- (IBAction)joinButton {
    struct userInfo localUser;

    localUser.firstName = firstName.text;
    localUser.lastName = lastName.text;
    localUser.username = username.text;
    localUser.email = emailAddress.text;
    localUser.ipAddress = localIPAddress.text;
    localUser.latitude = currentLocation.coordinate.latitude;
    localUser.longitude = currentLocation.coordinate.longitude;

    [myNetworkConnection registerWithServer:&localUser];
}

function handling the struct:

- (void)registerWithServer:(struct userInfo*)myUser {

    printf("First name is: %s", myUser.firstName);//error when compiling
}

the complier throws this error: request for member 'firstName' in something not a structure or union. Is that struct out of scope when I try to access it in the second function?

Eric de Araujo
  • 243
  • 7
  • 16
  • There's no reason to put Objective-C objects inside of structs. Don't do it. Either make an object to hold your data, put it in an NSDictionary, or convert everything to native C types like char* for your struct. – Jim Puls Jun 30 '09 at 17:01

2 Answers2

7

You are passing in a pointer to a struct. Use the -> operator, not the dot.

myUser->firstName
Matt K
  • 13,370
  • 2
  • 32
  • 51
  • that outputs 8a on the console, which isn't the inputed string. – Eric de Araujo Jun 30 '09 at 16:06
  • That's because it's an NSString, not a C string (char *). You can get a C string with a copy of the NSString's contents using the cStringUsingEncoding:, getCString:maxLength:encoding:, or UTF8String methods. The last may be the easiest: printf("%s\n", [myUser->firstName UTF8String]); – Matt K Jun 30 '09 at 16:08
  • To output an NSString with printf(), you have to convert it to a char* and use the %s format specifier. However, you can use %@ with NSLog, or with your own variant. http://cocoaheads.byu.edu/wiki/a-different-nslog – Quinn Taylor Jun 30 '09 at 16:27
  • Thanks. I should figured that one out. Since these structs are eventually going to get passed to the server I'm now thinking I should define the struct using char arrays instead of NSStrings. – Eric de Araujo Jun 30 '09 at 16:42
  • In this case, you probably want to make sure the structs are aligned to the same width on both client and server. – Matt K Jun 30 '09 at 17:40
  • I'm using the tpl library to handle serialization of the struts. tpl.sourceforge.net – Eric de Araujo Jun 30 '09 at 17:57
2

I can't help but think you should really make that a proper objective-C object with properties - more work but then it'll all behave better and live within the NSObject ecosystem.

Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150
  • right, and it will probably get to that. but right now I want to get comfortable sending a struct from the iPhone to a remote server. I imagine that in the future this will be an object and i'll copy it's properties in a struct to push out. – Eric de Araujo Jun 30 '09 at 17:03
  • Instead of writing a custom C server that pulls apart a binary structure built on an entirely different architecture from the server code, instead consider building out a simple REST style web service where you pass up new data as parameters instead... this will make life far, far easier for you in the long run. Otherwise start reading up on endian issues and how to adapt for differing int sizes in structures... – Kendall Helmstetter Gelner Jun 30 '09 at 20:26