Thread: Extracting an integer + real array from a pointer location

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    13

    Extracting an integer + real array from a pointer location

    I have a structure (void *cbdata) in memory created from Fortran. It contains:
    2 integers, then (up to) 100 doubles

    I need to extract the ints and doubles. I am doing this on the C side:

    Code:
      int *nvar = (int *) cbData;
      double *vars = (double *)((int)cbData + 8);
    but I get warnings:
    ex_nlp3.c(33): warning C4311: 'type cast': pointer truncation from 'void *' to 'int'
    ex_nlp3.c(33): warning C4312: 'type cast': conversion from 'int' to 'double *' of greater size

    Both warnings relate to the double line of code.

    How should I restructure this to eliminate the warnings?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,490
    Are you sure that your Fortran buffer is aligned correctly for accessing ints and doubles?

    Try
    double *vars = (double *)((char*)cbData + 8);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2015
    Posts
    13
    Quote Originally Posted by Salem View Post
    Are you sure that your Fortran buffer is aligned correctly for accessing ints and doubles?
    Yes, as I created the Fortran structure as follows:
    Code:
    type t_box
      sequence
      integer              :: nvar, ncall
      real(8)              :: vars(100)
    end type
    I didn't like to do this, I would have preferred vars to be an allocatable array (sized at runtime to nvar), however I wasn't sure how to access it on the C side.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting digits from an integer
    By AlexTank853 in forum C Programming
    Replies: 7
    Last Post: 09-29-2012, 03:53 PM
  2. Integer pointer to a 2-D array
    By Sylar Persie in forum C Programming
    Replies: 1
    Last Post: 06-16-2012, 03:13 AM
  3. Replies: 6
    Last Post: 11-20-2011, 05:11 PM
  4. Call the location in a pointer?
    By User Name: in forum C Programming
    Replies: 6
    Last Post: 06-10-2010, 11:29 PM
  5. integer pointer to an array[][][]
    By jordanguyoflove in forum C Programming
    Replies: 16
    Last Post: 12-23-2008, 12:19 PM

Tags for this Thread