Passing Pointer-to-struct to a function!!

This is a discussion on Passing Pointer-to-struct to a function!! within the C Programming forums, part of the General Programming Boards category; Hi everyone, Ah, great... I'm having a problem before I can even post the 'real' one... I'm using telnet, with ...

  1. #1
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60

    Passing Pointer-to-struct !!

    Hi everyone,

    Ah, great... I'm having a problem before I can even post the 'real' one... I'm using telnet, with W2k, and I can't copy my codes!!
    I was under W98 before, and there was a toolbar that enabled me to copy whatever was highlighted... the toolbar's gone!!
    Any idea??

    Ok, now let's get into the actual problem.
    I have to use pointer-to-struct array (yes, that's for an assignement), and an input file.
    I declared my pointers:
    Code:
    EMP *emp;
    initialized them to NULL
    Code:
    for ( ;emp*; emp++)
      *emp = NULL;
    but now I'm confused as to how do I pass them to other function by value, and by reference???
    Do I have to declare pointers-to-pointers and pass those when I want to pass by value? and pass the pointer-to-struct when I want to pass by reference?

    Those pointers are really getting to me cause I'm just getting more confused every time I have to use them!!!
    Last edited by Lau; 11-18-2002 at 12:11 PM.

  2. #2
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    emp* is also a syntax error, that should be *emp I guess.
    Yes, I did write it like that (*emp that is!!)... I didn't double check what I wrote, but I'm just still trying to figure out how to copy from telnet...

    Aren't
    Code:
    int  i;
    EMP *emp[MAXPOINTERS];
    for( i = 0; i < MAXPOINTERS; i++ ) {
      emp[i] = NULL;
    }
    and
    Code:
    for ( ;emp*; emp++)
      *emp = NULL;
    the same thing? Obivously not... but why is that?
    .... I think I got it: emp is an array, (that code was initially used for a string)... that's why it shouldn't work
    Last edited by Lau; 11-18-2002 at 12:42 PM.

  3. #3
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60

    about scanf...

    Can I use scanf() is that way?
    Code:
    scanf("%d", &emp[i].ID);

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Sure, but be sure to allocate (and eventually free) the array first:


    int i;
    EMP *emp[MAXPOINTERS];
    for( i = 0; i < MAXPOINTERS; i++ ) {
    emp[i] = malloc(sizeof(EMP));
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    Well Sebastiani,
    here's what i've got:
    Code:
    EMP * emp[SIZE];
    emp = (EMP *) malloc(SIZE * sizeof(EMP));
    is that correct?

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    No, but you can do:

    Emp * array = malloc(sizeof(EMP)*amount);

    for(i = 0; i < amount; i++){
    array[i].id = i+1;
    }
    Which makes it easier to free, too, that is, without a loop:

    delete(array);
    array = NULL;
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 04:45 AM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 04:25 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

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