place int pointer in data block

This is a discussion on place int pointer in data block within the C Programming forums, part of the General Programming Boards category; This relates to my last post about using an offset to select a sort field from a linked list node. ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    place int pointer in data block

    This relates to my last post about using an offset to select a sort field from a linked list node.

    If the the field selected is an int, and the offset into the node struct is 20, how can I get this int out? I would have thought it would have been as simple as:
    Code:
    node *np;
    int *ptr=&np+20;
    or
    Code:
    int *ptr=(int*)np+20;
    or about 45 other things I've tried. Placing a char* is as simple as:
    Code:
    node *np;
    char *ptr=(char*)np+20;
    I guess I don't work with numbers enough...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The only thing I've found which works is this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct _node {
    	char f1[16];
    	char f2[16];
    	int f3;
    	struct _node *next;
    } node;
    
    struct num {
    	int x;
    };
    
    int main () {
    	char *cp;
    	struct num *ptr;
    	node *test=malloc(sizeof(node));
    	strcpy(test->f2,"hello world");
    	test->f3=66; 
    	cp=(char*)test+32;
    	ptr=(struct num*)cp;	
    	printf("%s %d %d\n",(char*)test+16, sizeof(node), ptr->x);
    }
    and I'm sure there must be a simpler way.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Wow, I finally got it:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct _node {
    	char f1[16];
    	char f2[16];
    	int f3;
    	struct _node *next;
    } node;
    
    int main () {
    	char *cp;
    	int x;
    	node *test=malloc(sizeof(node));
    	strcpy(test->f2,"hello world");
    	test->f3=66; 
    	cp=(char*)test+32;
    	x=*((int*)cp);	
    	printf("%s %d\n",(char*)test+16, x);
    	return 0;
    }
    but please, if there are any alternatives I'd love to know what I missed.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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