Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using structure with pointers 1

Status
Not open for further replies.

robUK2

Programmer
Mar 10, 2008
57
TH
Hello,

Compiling on windows using GNC GCC Codeblocks.

I have a structure as follows.

I am using wondering if this is the correct way to assign a value to the index which is a pointer.

Code:
struct lineService
{
    int lineNumber;
    char lineName[80];
    int *index;
} line1;

int lineIndex = 10;
line1.index = lineIndex;

Just another question. Is there any response to having a pointer like this? What is the most common situation where one would be used?

Many thanks,

 
No, your compiler should be giving you some sort of warning about that.

I'm not sure what you want to do or what that struct is used for, but you need to assign a pointer to a pointer, not an int to a pointer. Either of these will work:
Code:
line1.index = &lineIndex;
Code:
line1.index = malloc( sizeof(int) );
*(line1.index) = lineIndex;
 
Hello,

Your technique worked for me. I have tried both.

However, I am just experimenting with struct's and wondering when you would normally use a pointer liket his?

Thanks,

Steve
 
Two common things you'd use pointers for in a struct would be to point to the next node in a linked list, or to have a variable sized array that is created at runtime.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top