Thread: Help with pointer arrays please

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    16

    Unhappy Help with pointer arrays please

    Hello,

    i am doing a module at uni that I need your help with. ( in which I'm building a CPU simulator I am writting for my final year project).

    I'm trying to make the simulator accept a file that defines each instruction that the simualotor supports. It has a format specified in the file "intSet.txt". This file detials the exact specification of a given instruction in my program and i was wondering how i could make my program understand the instruciton operation that has been specified by the "RTL" section of the file.

    The C program i have attatched is an attempt at reading in each part of the file into various arrays (that would later form part of a structure). unfortunately the 2Dpointer arrays dont seem to work (the early elements of the pointer array seem to get over written) what mistake have i made?

    Any help would be great especially in figuring out how to make my program "understand" RTL.

    Thanks for your help
    Garry
    Last edited by Nutcasey; 12-14-2003 at 04:28 PM.

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    16
    here is the c file:

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    You're not allocating any memory for your tokenised strings.

    Code:
    	while ((element[q++] = strtok(point_string, search_field)) != NULL)
    	{
    		point_string = NULL;	/* only needed for the first time */
    	//	printf("element[%d] = %s\n", t, element[t]);
    	}
    Should be
    Code:
    char *p;
    while ( ( p = strtok(point_string, search_field)) != NULL) {
        element[q] = malloc( strlen(p) + 1 );
        strcpy( element[q], p );
        q++;
        point_string = NULL;
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. pointer arrays!!!
    By condorx in forum C Programming
    Replies: 1
    Last Post: 05-14-2002, 08:55 AM
  4. pointer arrays
    By condorx in forum C Programming
    Replies: 3
    Last Post: 05-03-2002, 09:04 PM
  5. Replies: 4
    Last Post: 11-05-2001, 02:35 PM