codescracker


objective c

Objective-C Structures



« Previous Tutorial Next Tutorial »


Structure is a user-defined data type available in Objective-C, that allows you to combine data items of different kinds. Structure are generally used to represent a record.

Let's suppose, if you want to keep track of your students, then you might want to track these attributes about each students:

Define a Structure in Objective-C

To define a structure in Objective-C, you have to use the struct keyword. The struct statement simply defines a new data type, with more than one member for your Objective-C program. Here is the general form of the struct statement in Objective-C:

struct [structure tag]
{
	member definition;
	member definition;
	.
	.
	.
	member definition;
   
} [structure variable(s)];

Here the structure tag is optional and each member definition is simply a normal variable definition, like:

or any other valid variable definition. Structure variable is optional, you can define one or more structure variable at the end of structure's definition, you can also define it later in the program. Here is the way, you can declare the STUDENTS structure:

struct STUDENTS
{
	int rollno;
	NSString *name;
	NSString *branch;
	NSSString *batch;
} stud;

Access Structure Members in Objective-C

To access any structure member in Objective-C, you have to use the member access operator, that is, dot (.) operator. The member access operator is simply coded as a period between the structure variable name and the structure member that we want to access. You have to use the struct keyword in defining the variables of structure type.

Objective-C Structure Example

Here is an example program, illustrates the concept of structure in Objective-C practically

/* Objective-C Structure - Example Program */
		
#import <Foundation/Foundation.h>

struct STUDENTS
{
	int rollno;
	NSString *name;
	NSString *branch;
	NSString *batch;
};
 
int main()
{
	struct STUDENTS stud1;        /* Declared stud1 of type STUDENTS */
	struct STUDENTS stud2;        /* Declared stud2 of type STUDENTS */
	
	/* stud1 specification */
	stud1.rollno = 12;
	stud1.name = @"Devraj Singh"; 
	stud1.branch = @"Computer Science & Engineering";
	stud1.batch = @"C";

	/* stud2 specification */
	stud2.rollno = 09;
	stud2.name = @"Alok Singh";
	stud2.branch = @"Computer Science & Engineering";
	stud2.batch = @"B";
 
	/* now print stud1 info */
	NSLog(@"stud1 Roll Number = %d\n", stud1.rollno);
	NSLog(@"stud1 Name = %@\n", stud1.name);
	NSLog(@"stud1 Branch = %@\n", stud1.branch);
	NSLog(@"stud1 Batch = %@\n\n", stud1.batch);

	/* now print stud2 info */
	NSLog(@"stud2 Roll Number = %d\n", stud2.rollno);
	NSLog(@"stud2 Name : %@\n", stud2.name);
	NSLog(@"stud2 Branch : %@\n", stud2.branch);
	NSLog(@"stud2 Batch : %@\n", stud2.batch);

	return 0;
}

When the above code is compile and executed, it will produce the following output:

2014-03-14 04:20:07.947 demo[20591] stud1 Roll Number = 12
2014-03-14 04:20:07.947 demo[20591] stud1 Name = Devraj Singh
2014-03-14 04:20:07.947 demo[20591] stud1 Branch = Computer Science & Engineering
2014-03-14 04:20:07.947 demo[20591] stud1 Batch = C

2014-03-14 04:20:07.947 demo[20591] stud2 Roll Number = 09
2014-03-14 04:20:07.947 demo[20591] stud2 Name = Alok Singh
2014-03-14 04:20:07.947 demo[20591] stud2 Branch = Computer Science & Engineering
2014-03-14 04:20:07.947 demo[20591] stud2 Batch = B

Structures as Function Arguments in Objective-C

You are free to pass a structure as function argument in Objective-C. The process is similar to, passing of any other variable or pointer. You can access the structure variables in the similar way as you have access in the above Objective-C program. Here is an example program, demonstrating this concept in Objective-C:

/* Objective-C Structures - Example Program */
		
#import <Foundation/Foundation.h>

struct STUDENTS
{
	int rollno;
	NSString *name;
	NSString *branch;
	NSString *batch;
};

@interface SampleClass:NSObject

- (void) printStud:(struct STUDENTS) stud;

@end

@implementation SampleClass 

- (void) printStud:(struct STUDENTS) stud
{
	NSLog(@"Stud Roll Number = %d\n", stud.rollno);
	NSLog(@"Stud Name = %@\n", stud.name);
	NSLog(@"Stud Branch = %@\n", stud.branch);
	NSLog(@"Stud Batch = %@\n\n", stud.batch);
}
@end

int main()
{
	struct STUDENTS stud1;
	struct STUDENTS stud2;
   
	stud1.rollno = 1;
	stud1.name = @"Devraj Singh"; 
	stud1.branch = @"Computer Science & Engineering";
	stud1.batch = @"C";
	
	stud2.rollno = 09;
	stud2.name = @"Alok Singh";
	stud2.branch = @"Computer Science & Engineering";
	stud2.batch = @"B";
	
	SampleClass *sampleClass = [[SampleClass alloc]init];
	[sampleClass printStud: stud1];
	[sampleClass printStud: stud2];

	return 0;
}

When the above code is compile and executed, it will produce the following result:

2014-03-14 04:34:45.725 demo[8060] Stud Roll Number = 12
2014-03-14 04:34:45.725 demo[8060] Stud Name = Devraj Singh
2014-03-14 04:34:45.725 demo[8060] Stud Branch = Computer Science & Engineering
2014-03-14 04:34:45.725 demo[8060] Stud Batch = C

2014-03-14 04:34:45.725 demo[8060] Stud Roll Number = 09
2014-03-14 04:34:45.725 demo[8060] Stud Name = Alok Singh
2014-03-14 04:34:45.725 demo[8060] Stud Branch = Computer Science & Engineering
2014-03-14 04:34:45.725 demo[8060] Stud Batch = B

Pointers to Structures in Objective-C

You are free to define pointers to structures in Objective-C, which can be done in very similar way as you define pointer to any other variable. Here is a code fragment showing, how to do this:

struct STUDENTS *struct_ptr;

Now, you can store the address of structure variable in the pointer variable as defined above. To find the address of structure variable, just place the & operator before the structure's name like this:

struct_ptr = &stud1;

And to access the member of structure using a pointer to that structure, use the -> operator like this:

struct_ptr->rollno;

Example

Here is the complete program to illustrates the concept of pointer to structure in Objective-C:

/* Objective-C Structure - Example Program */
		
#import <Foundation/Foundation.h>

struct STUDENTS
{
	int rollno;
	NSString *name;
	NSString *branch;
	NSString *batch;
};

@interface SampleClass:NSObject
- (void) printStud:(struct STUDENTS *) stud;
@end

@implementation SampleClass 

- (void) printStud:(struct STUDENTS *) stud
{
	NSLog(@"Stud Roll Number = %d\n", stud->rollno);
	NSLog(@"Stud Name = %@\n", stud->name);
	NSLog(@"Stud Branch = %@\n", stud->branch);
	NSLog(@"Stud Batch = %@\n\n", stud->batch);
}
@end

int main()
{
	struct STUDENTS stud1;
	struct STUDENTS stud2;
	
	stud1.rollno = 12;
	stud1.name = @"Devraj Singh"; 
	stud1.branch = @"Computer Science & Engineering";
	stud1.batch = @"C";
	
	stud2.rollno = 09;
	stud2.name = @"Alok Singh";
	stud2.branch = @"Computer Science & Engineering";
	stud2.batch = @"B";
	
	SampleClass *sampleClass = [[SampleClass alloc]init];
	[sampleClass printStud:&stud1];
	[sampleClass printStud:&stud2];
	
	return 0;
}

When the above code is compile and executed, it will produce the following result:

2014-03-14 04:38:13.942 demo[20745] Stud Roll Number = 12
2014-03-14 04:38:13.942 demo[20745] Stud Name = Devraj Singh
2014-03-14 04:38:13.942 demo[20745] Stud Branch = Computer Science & Engineering
2014-03-14 04:38:13.942 demo[20745] Stud Batch = C

2014-03-14 04:38:13.942 demo[20745] Stud Roll Number = 09
2014-03-14 04:38:13.942 demo[20745] Stud Name = Alok Singh
2014-03-14 04:38:13.942 demo[20745] Stud Branch = Computer Science & Engineering
2014-03-14 04:38:13.942 demo[20745] Stud Batch = B

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test