Question about pointer to structures

This is a discussion on Question about pointer to structures within the C Programming forums, part of the General Programming Boards category; Hi, when passing a structure to a function that'll modify the structure itself such as scan...etc it is advised to ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    46

    Question about pointer to structures

    Hi,
    when passing a structure to a function that'll modify the structure itself such as scan...etc it is advised to use pointers to that structure as the function parameters which saves lots of memory because the program wont have to copy the whole structure to the function.It's also know that when dealing with functions to modify arrays you dont need to do any of the pointer work as it is already done.The question is what if i have some structure and i formed an array of that structure and want to make a function that modifies the whole array. it.Do i need any pointers to save memory or make the program more efficient or do arrays take care of it?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    No, an array of structures is the same as an array of any plain old data. You're getting reference to the structure passed to the function. You don't need to make a pointer to the array. You seem to be confused about the use of pointers, though. They don't just "save memory" they're completely nessassary if you want to make any changes to the structure at all. If you pass by value and make a copy of your data, you can edit it all you want and not change the original, at all.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    46
    Quote Originally Posted by SlyMaelstrom
    No, an array of structures is the same as an array of any plain old data. You're getting reference to the structure passed to the function. You don't need to make a pointer to the array. You seem to be confused about the use of pointers, though. They don't just "save memory" they're completely nessassary if you want to make any changes to the structure at all. If you pass by value and make a copy of your data, you can edit it all you want and not change the original, at all.
    Thanks for the input.But I guess I understand pointers well but what is confusing me is that if you want to make a function that edits an array no need for pointers.but if you want to make a function that edits a structure you need pointers.What if a want a function to edit an array of structures???

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Quote Originally Posted by KidMan
    What if a want a function to edit an array of structures???
    I thought I answered that. You pass it like an array of any other type. It works the same.
    Code:
    #include <stdio.h>
    
    struct foo {
       int num1;
    };
    
    void setNum(struct foo obj1) {  // By value
         obj1.num1 = 10;
    }
    
    void setNum(struct foo *obj1) {  // By address
         obj1->num1 = 10;
    }
    
    void setNum(struct foo obj[], int size) {  // By array (which is also by address)
         int i;
         for (i = 0; i < size; i++)
            obj[i].num1 = i;
    }
    
    int main(void) {
       struct foo obj[2];
       obj[0].num1 = 5;
       obj[1].num1 = 7;
       
       printf("obj[0].num1 = %d\n", obj[0].num1);
       setNum(obj[0]);   // The first function
       printf("obj[0].num1 = %d\n", obj[0].num1);
       setNum(&obj[0]);  // The overloaded function for pointers
       printf("obj[0].num1 = %d\n", obj[0].num1);
       setNum(obj, 2);   // The overloaded function for arrays
       printf("obj[0].num1 = %d\nobj[1].num1 = %d\n", obj[0].num1, obj[1].num1);
       
       return 0;
    }
    
    /* Output:
    obj[0].num1 = 5
    obj[0].num1 = 5
    obj[0].num1 = 10
    obj[0].num1 = 0
    obj[1].num1 = 1 */
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    46
    Now I understand.Thanks SlyMaelstrom

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  2. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  3. a pointer to a function question..
    By transgalactic2 in forum C Programming
    Replies: 17
    Last Post: 10-21-2008, 12:47 PM
  4. Pointers to structures - Beginner question
    By RobJ in forum C Programming
    Replies: 6
    Last Post: 04-10-2006, 06:57 PM
  5. Simple pointer question
    By jayznz in forum C Programming
    Replies: 2
    Last Post: 04-05-2006, 12:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21