×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

using structure with pointers

using structure with pointers

using structure with pointers

(OP)
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,

 

RE: using structure with pointers

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;

RE: using structure with pointers

(OP)
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

RE: using structure with pointers

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close