Thread: help making a reference to a pointer

  1. #1
    Registered User
    Join Date
    Apr 2006

    help making a reference to a pointer

    Hi all,

    I'm trying to keep use a reference to a pointer within a nested structure, but it isn't working. I looked in a couple books, didn't see anything that helped, so I am posting here. Hopefully someone can spot what I am doing wrong quite easily.

    I have two structures:

    typedef struct _smsBuffer
            SMSHeader *pSmsHeader;
            SMS_DATA *pSmsRecord;
    } t_smsBuffer;
    typedef struct _smsanal
            t_object x_obj; 
            t_canvas *canvas;
            t_symbol *s_filename;
            t_int i_frame, i_frameSource, synthBufPos;
            t_int nframes;
            t_float *synthBuf;
            t_float f;
    	FILE *pSmsFile; 
            SYNTH_PARAMS synthParams;
            t_smsBuffer smsBuf;
    } t_smsanal;
    In smsBuf, pSmsRecord will be an array of frames of data records, while the header gives overall info about the frames. Now, in my function for opening a file, I get a pointer to the t_smsanal structure as x:

    static void smsanal_open(t_smsanal *x, t_symbol *filename)
            SMSHeader *pHeader = (SMSHeader *)x->smsBuf.pSmsHeader;
            // this workes:
            //GetSmsHeader (filename->s_name, &x->smsBuf.pSmsHeader, &x->pSmsFile)
            // but this doesn't (crashes):
            GetSmsHeader (fullname->s_name, &pHeader, &x->pSmsFile)
    I'd really like to use the simple pointer to the header, since I will constantly need to be getting data out of it in later code. But nothing I seem to try works.

    Thanks if you can help,

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Banks of the River Styx
    SMSHeader *pHeader = (SMSHeader *)x->smsBuf.pSmsHeader;
    Why is that cast there?

    As for smsanal_open, your "works" and "doesn't work" are not equivalent. One might modify the structure, the other might modify that local variable, whoch could have consequences later. (We'd need to see more code...) Are you sure GetSmsHeader crashes? And if it's the crashing function, why not post that code?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Apr 2006
    As for the (SMSHeader *) cast, it is only there because I was trying to see if it would help, but didn't. I guess it isn't doing anything.

    Here is GetSmsHeader:
    int GetSmsHeader (char *pChFileName, SMSHeader **ppSmsHeader,
                      FILE **ppSmsFile)
    	int iHeadBSize, iRecordBSize, nRecords;
    	int iMagicNumber;
    	/* open file for reading */
    	if ((*ppSmsFile = fopen (pChFileName, "r")) == NULL)
    		return (SMS_NOPEN);
    	/* read magic number */
    	if (fread ((void *) &iMagicNumber, (size_t) sizeof(int), (size_t)1, 
    	           *ppSmsFile) < (size_t)1)
    		return (SMS_RDERR);
    	if (iMagicNumber != SMS_MAGIC)
    		return (SMS_NSMS);
    	/* read size of of header */
    	if (fread ((void *) &iHeadBSize, (size_t) sizeof(int), (size_t)1, 
    		*ppSmsFile) < (size_t)1)
    		return (SMS_RDERR);
    	if (iHeadBSize <= 0)
    		return (SMS_RDERR);
      /* read number of data Records */
      if (fread ((void *) &nRecords, (size_t) sizeof(int), (size_t)1, 
    	         *ppSmsFile) < (size_t)1)
    		return (SMS_RDERR);
    	if (nRecords <= 0)
    		return (SMS_RDERR);
      /* read size of data Records */
    	if (fread ((void *) &iRecordBSize, (size_t) sizeof(int), (size_t)1, 
    	           *ppSmsFile) < (size_t)1)
    		return (SMS_RDERR);
    	if (iRecordBSize <= 0)
    		return (SMS_RDERR);
    	/* allocate memory for header */
    	if (((*ppSmsHeader) = (SMSHeader *)malloc (iHeadBSize)) == NULL)
    		return (SMS_MALLOC);
    	/* read header */
    	rewind (*ppSmsFile);
    	if (fread ((void *) (*ppSmsHeader), 1, iHeadBSize, *ppSmsFile) < iHeadBSize)
    		return (SMS_RDERR);
    	/* set pointers to variable part of header */
    	if ((*ppSmsHeader)->nLoopRecords > 0)
    		(*ppSmsHeader)->pILoopRecords = (int *) ((char *)(*ppSmsHeader) + 
    	if ((*ppSmsHeader)->nSpecEnvelopePoints > 0)
    		(*ppSmsHeader)->pFSpectralEnvelope = 
    			(float *) ((char *)(*ppSmsHeader) + sizeof(SMSHeader) + 
    			           sizeof(int) * (*ppSmsHeader)->nLoopRecords);
    	if ((*ppSmsHeader)->nTextCharacters > 0)
    		(*ppSmsHeader)->pChTextCharacters = 
    			(char *) ((char *)(*ppSmsHeader) + sizeof(SMSHeader) + 
    			sizeof(int) * (*ppSmsHeader)->nLoopRecords +
    			sizeof(float) * (*ppSmsHeader)->nSpecEnvelopePoints);
    	return (SMS_OK);			
    I will admit that I am not sure why a double pointer is used, but that is how the code was handed to me. I'm not sure I understand what you wrote about modifying the local variable; the local variable is a pointer to the structure.

    thanks for the help,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pass by pointer, reference, value.
    By Lithorien in forum C++ Programming
    Replies: 8
    Last Post: 02-25-2005, 10:03 AM
  2. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  3. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Replies: 2
    Last Post: 05-04-2003, 11:55 AM
  5. C++ pointer vs reference
    By C-Dumbie in forum C++ Programming
    Replies: 4
    Last Post: 11-04-2002, 04:08 AM