[PD] free C lesson

Jonathan Wilkes jancsika at yahoo.com
Mon Aug 17 17:52:27 CEST 2015


Thanks, I think I'm getting it.
So is char* guaranteed to be the same width as sizeof(t_word)?  If so, are you just using it as a shorthand?

Thanks,
Jonathan


 


     On Monday, August 17, 2015 11:31 AM, Miller Puckette <msp at ucsd.edu> wrote:
   

 Here's an example...

#include <stdio.h>

float foo[2];

main()
{
    printf("foo = %p\n", foo); 
    printf("incremented as float: %p\n", foo+1);
    printf("incremented as (char *): %p\n", ((char *)foo)+1);
}

--->

foo = 0x601038
incremented as float: 0x60103c
incremented as (char *): 0x601039

Adding an integer to a pointer "increments" it - the effect depends on the type
of pointer.  Another way to think of it is that foo[1], say, is semantically
identical to *(foo+1).

cheers
Miller

On Mon, Aug 17, 2015 at 03:10:35PM +0000, Jonathan Wilkes via Pd-list wrote:
> But we're dealing with an array of t_words, so onset*sizeof(t_word) is what we want anyway, no?
> -Jonathan 
> 
> 
>      On Monday, August 17, 2015 10:55 AM, Claude Heiland-Allen <claude at mathr.co.uk> wrote:
>    
> 
>  On 17/08/15 15:36, Jonathan Wilkes via Pd-list wrote:
> > Hi list,Wondering if someone will give me a free lesson in C programming.
> > In g_traversal.c, there's some code to retrieve a float from a t_word* vec.  It looks like this:
> > *(t_float *)(((char *)vec) + onset));
> > Why does vec need to be cast to char*?  t_word has to be as big as the largest member of the union, and the largest member has to be the same size as char*, right?  (Otherwise we'd have big problems...)
> 
> aiui pointer arithmetic is in increments of sizeof(pointee)
> if onset is measured in bytes (I don't know if it is in this case, but 
> it looks likely), then you need to have a pointer to bytes for the 
> addition to be meaningful.  vec is already a pointer, but adding onset 
> to a t_word* would offset the address by onset*sizeof(t_word) bytes
> 
> 
> _______________________________________________
> Pd-list at lists.iem.at mailing list
> UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list
> 
> 
>  

> _______________________________________________
> Pd-list at lists.iem.at mailing list
> UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list



  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puredata.info/pipermail/pd-list/attachments/20150817/1fb3cce5/attachment.html>


More information about the Pd-list mailing list