Thread: Pointing to a vectors x coordinate located within a structure

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    18

    Pointing to a vectors x coordinate located within a structure

    Can someone explain to me what I am doing wrong here? Cam is a pointer to a structure and viewpoint is a vector located within the struct. I am trying to read in from a file the coordinates for the vector. I have also tried &cam->view_point->x as well as &cam.view_point.x and it tells me that I am requesting something not in a struct
    Code:
    count= fscanf(in,"%d %d %d", &cam->view_point.x, &cam->view_point.y,&cam->view_point.z);

  2. #2
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    I tried, but my telepathic abilities are not strong enough to reach your mind. Are you wearing a tinfoil hat?

    Without knowing how cam and view_point are defined, or the exact error, any explanation is simply futile.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    By vector, do you mean array, or some other sort of structure? I would not like to see what scanf() would do to a c++ vector. Probably give it its best shot and blow up your program.

  4. #4
    Registered User
    Join Date
    Nov 2014
    Posts
    18
    Sorry forgot to add that info. But the error says request for member 'x' in something not a struct or union. scanf is just one part of my program. I need to print the x y and z of the viewpoint for another minor portion.
    cam is defined as
    Code:
    typedef struct camera_type{
       int    cookie;
       char   name[NAME_LEN];
       int    pixel_dim[2];    /* Projection screen size in pixels */
       double world_dim[2];    /* Screen size in world coords      */
       vec_t  view_point;      /* Viewpt Loc in world coords       */
       irgb_t *pixmap;         /* Build image here                 */
    }  camera_t;
    and viewpoint is defined as
    Code:
    typedef struct vec_type{
       double x;
       double y;
       double z;
    } vec_t;

  5. #5
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    And how is the variable cam declared? (This feels a bit like we're pulling your teeth, you know.)

    One thing I can immediately tell, is that your coordinates are doubles (scan pattern %lf), not ints (%d).

  6. #6
    Registered User
    Join Date
    Nov 2014
    Posts
    18
    Cam is the pointer to the camera_t structure. Dont mean for this to be a hassle I apologize I am fairly new to programming and just trying to figure things out for my project as my professor does not help or reply to emails very well
    Code:
    camera_t *cam=malloc(sizeof(camera_t));

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Um. Ok. I don't know where to start.
    Ok.

    Your structuires are indeed structures, and I have nothing bad to say about them,

    However (I really do try to be nice) that fscan call is an abomination!!! It looks like (and I imagine this is correct) that you kept hacking away at it hoping it'd eventually compile.

    Ok, that was rude. Helpful hat on now!

    If you normally access your structures as pointers (i.e. use -> ratner than ) then there's no need at all to to put \n & in front pof them in scanf. My guerss is though that you though that'd turn them into structure pointers. That's wrong -- C operator precedence (C++ Operator Precedence - cppreference.com) puts "&" very high up the list, so your hapless structure got pointerised -- but ypu want a pointer to the member, right? You can override operator precedence with paranthesiss, but alll you needed to do was:

    Code:
     &cam.view_point..x
    "." beats "&" in the precedence table, yay.

    The reaspn the compiler told your structures aren't strictires is because it was confuuuuused. You'd done something very odd and compilers often flake a bit in that situation.

    FInally omigod noes!! You're trying to read doubles using integer specifiers. On almost all machines, integers(%d) are 32-bit and doubles are-64-bits. What do you think would happen? I don't know -- it varies. Use %f fo scanfingr doubles. I know that sounds silly since %f sounds like it is for float, whivh is also 32-bit, but scanf has well definied behaviour i0n this area and will treat your double properly.


    Finally.... like I said, this was pretty bad code. But scanf is one of the first things we learn, and no one tells us to go and learn more. Go forth and learn

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by Nominal Animal View Post
    One thing I can immediately tell, is that your coordinates are doubles (scan pattern %lf), not ints (%d).

    Yrah, you're right, I retract my advice about using %f. It'll work, but as a rule I try to avoid sneaky implicit casts. Control freak.....

    I read a while ago that %f was float and for double, and %lf was for long double. I can't find anything credible thst backs that up (I did try ;-).

  9. #9
    Registered User
    Join Date
    Nov 2014
    Posts
    18
    Yeah that was not my choice haha all just part of a project. So what exactly does the two dots do? And if i wanted to print it would i just do the following below? I get the same error as previously when I do this but without the two dots

    Code:
    fprintf(out,"%lf %lf %lf",cam->view_point..x,cam->view_point..y,cam->view_point..z);

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Oh no! I'm really sorry, that was just a typo!! I meant:

    Code:
    	
    fprintf(out,"%lf %lf %lf",cam->view_point.x,cam->view_point.y,cam->view_point.z);
    Sorry about that. I was staring and staring at your code and trying to work out what on earth they'd do.
    Last edited by smokeyangel; 02-08-2015 at 10:09 PM. Reason: one more dot esxaped

  11. #11
    Registered User
    Join Date
    Nov 2014
    Posts
    18
    Youre good youre good! I was confused for a second. What if i was gonna make another vector pointer lets say called V1. And i wanted to call a function that takes in three vectors, V1 being the first , the viewpoint vector being the second and, V1 being the third also. How would I pass the view point vector in shouldnt it be
    Code:
    V1=vec_diff(V1,cam->view_point,V1);
    Because I try this and I get an error saying I am requesting for viewpoint when it is not in a structure but it is in a structure is it not? This confuses me

  12. #12
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    smokeyangel, you okay and sober? Lots of typos..

    bgibs24, you should always provide enough code to compile. It does not have to be a full program (no need to have main()), but it should compile and produce the exact error you're seeing in your project.

    As to your original problem, consider this example program:
    Code:
    #include <stdlib.h>
    #include <stdint.h>
    #include <string.h>
    #include <stdio.h>
    #include <errno.h>
    
    typedef uint32_t    pixel;
    
    typedef struct {
        double  x;
        double  y;
    } vec2d;
    
    typedef struct {
        double  x;
        double  y;
        double  z;
    } vec3d;
    
    typedef struct {
        double  x;
        double  y;
        double  z;
        double  w;
    } vec4d;
    
    typedef struct {
        int     screen_width;
        int     screen_height;
        vec3d   location;
        vec3d   target;
        vec3d   up;
        vec3d   right;
        pixel  *image;
    } camera;
    
    int main(void)
    {
        camera  cam1;
        camera *cam2;
    
        cam2 = malloc(sizeof *cam2);
        if (cam2 == NULL) {
            fprintf(stderr, "Not enough memory.\n");
            return EXIT_FAILURE;
        }
    
        if (fscanf(stdin, " %lf %lf %lf", &(cam1.location.x), &(cam1.location.y), &(cam1.location.z)) != 3) {
            fprintf(stderr, "No cam1 location specified.\n");
            return EXIT_FAILURE;
        }
    
        if (fscanf(stdin, " %lf %lf %lf", &(cam2->location.x), &(cam2->location.y), &(cam2->location.z)) != 3) {
            fprintf(stderr, "No cam2 location specified.\n");
            return EXIT_FAILURE;
        }
    
        printf("cam1 location is (%g, %g, %g)\n", cam1.location.x, cam1.location.y, cam1.location.z);
        printf("cam2 location is (%g, %g, %g)\n", cam2->location.x, cam2->location.y, cam2->location.z);
    
        return EXIT_SUCCESS;
    }
    Note that %lf is the pattern to scan a double, but you use %f to print one. When you print a float (which you can scan using %f), it is actually promoted to double type.

    To scan a long double, you use %Lf or %llf , and %Lf to print.

    There are actually quite a few formats and specifiers you can use too, documented at the printf() and scanf() man pages, but these two happen to be the two densest man pages, and so are not the easiest to read for new programmers. But they do help when you write new code or test functionality.

    Questions?

  13. #13
    Registered User
    Join Date
    Nov 2014
    Posts
    18
    This was actually really helpful thank you! But now I know to put more code up Ill be sure to do that next time for sure

  14. #14
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Oh dear . No, not exactly ok or sober. It's a long story, but I have a few broken ribs and am so doped up I can barely see the keyboard.

    My apologies - had I realised that this had been noticed I would have stopped posting straight away! It just.... it's distracting, and it's something I'm good at. Don't worry, there's a big internet out there to keep my mind off it,

    Sorry if I embarassed myself. Take care, and see you guys when I'm typo free.

  15. #15
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by bgibs24 View Post
    How would I pass the view point vector in shouldnt it be
    Code:
    V1=vec_diff(V1,cam->view_point,V1);
    It depends on the function. For example, if you had
    Code:
    vec3d vec_diff(const vec3d from, const vec3d to)
    {
        const vec3d result = { to.x - from.x, to.y - from.y, to.z - from.z };
        return result;
    }
    
    double vec_normalize(vec3d *const vec)
    {
        const double retval = sqrt(vec->x*vec->x + vec->y*vec->y + vec->z*vec->z);
        if (retval > 0.0) {
            vec->x /= retval;
            vec->y /= retval;
            vec->z /= retval;
            return retval;
        } else {
            vec->x = 0.0;
            vec->y = 0.0;
            vec->z = 0.0;
            return 0.0;
        }
    }
    you can use them using
    Code:
        vec3d a, b, c, d;
    
        a = vec_diff(b, c);
        d = a;
        vec_normalize(&d);
    So, if a function asks for vec3d *, you supply &(cam->view_point).
    If a function asks for vec3d, you supply cam->view_point.

    Quote Originally Posted by smokeyangel View Post
    It's a long story, but I have a few broken ribs
    Ouch! I hope they heal fast!

    Quote Originally Posted by smokeyangel View Post
    My apologies
    Your intentions were good, so no need to worry about that.

    Quote Originally Posted by smokeyangel View Post
    it's distracting, and it's something I'm good at.
    I hear you. It's the same reason I like to help here; it is distracting, and I for one get a kick out of helping others.

    Take a good rest, and I hope you get well soon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. .DLL error - Procedure entry point could not be located
    By HelpfulPerson in forum C Programming
    Replies: 6
    Last Post: 10-24-2013, 06:30 PM
  2. Data Structure to store unlimited dynamic vectors
    By m3rk in forum C++ Programming
    Replies: 8
    Last Post: 04-22-2009, 06:12 AM
  3. Replies: 4
    Last Post: 12-19-2005, 03:01 PM
  4. Trying to convert vectors from cartesian coordinate
    By FALSTON01 in forum C++ Programming
    Replies: 1
    Last Post: 10-20-2005, 05:17 PM
  5. where is pow() located?
    By thinhare in forum C Programming
    Replies: 7
    Last Post: 01-10-2005, 04:42 PM