How do you reference a lccation in an array

This is a discussion on How do you reference a lccation in an array within the C Programming forums, part of the General Programming Boards category; i want to create an array of fill characters so I do this: Code: for (z=0; z<l_length; z=z+1) //639 { ...

  1. #1
    Unregistered
    Guest

    How do you reference a lccation in an array

    i want to create an array of fill characters so I do this:

    Code:
    for (z=0; z<l_length; z=z+1) //639
    	{
    
    			for(i=0;i<addr2;i++) bufferarray[i]=fill;
    			bufferarray[addr2]=0;
    			{
    				printf("%s", bufferarray ); 
    			} //end for
    
    	} //end for
    Now that I have my array, I want to go back and reference location bufferarray[14] and place data into this spot and then after the data is finished, fill the rest of the array with the same fill character.

    xxxxxxxxxxxxsomedataxxxxxxxxxxxxx

    where x is the fill character and somedata represents...some data. i want to be able to take in the next set of data, which contains another location in the array where data needs to be written to and have that data entered in the same manner.

    Right now, all I seem able to do is enter the new data into another array of size l_length so instead of one great big array I get 100's of arrays.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    Do you have an example of what "somedata" is?

  3. #3
    Unregistered
    Guest
    some data is a list which indicates where in the array the data needs to go. so something like:
    4 123
    22 789
    7 456
    16 123
    10 789
    19 456

    so my output in the array should be

    xxx123456789xxx123456789

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    So the first number is the position in the array, and the 2nd is the number you want to insert into your "xxx" filled buffer?

  5. #5
    Unregistered
    Guest
    yes...exactly

    first I should have an array of all x's then filled with the inputs

    xxxxxxxxxxxxxxxxxxxxxxxx
    xxx123456789xxx123456789

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    > first I should have an array of all x's then filled with the inputs
    Yes, sounds like a good idea to me

    Taking your first line
    > 4 123
    convert the 4 to an integer (say pos)
    copy the 123 to a temp string (say temp)

    Then do
    strncpy( &buff[pos], temp, strlen(temp) );

  7. #7
    Unregistered
    Guest
    what is temp in your strncpy code? is it a temporary array that I have to create? if so, why can't i put it back into the original array that i created of all x's named bufferarray?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    temp is a char array which holds the "123" between being read from your file, and being written to bufferarray

  9. #9
    Unregistered
    Guest
    I think i did what you recommended me to do, but I can't seem to get the rest of the bufferarray to fill with x's. it stops outputting after the final set of characters.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    Post what you have so far then

  11. #11
    Unregistered
    Guest
    and one more thing, how do I declare temp if the length of it varies from one input string to another. so suppose

    instead of

    4 123
    22 789
    7 456
    16 123
    10 789
    19 456

    I really had

    4 1234
    22 789
    8 56
    16 1234
    10 789
    20 56

    xxx123456789xxx123456789xxxxxxxxxxxxxxxx

  12. #12
    Unregistered
    Guest
    Code:
    	for (i=9, m=0; i<(9+a+a); i=i+1, m++)
    				{
    				//	printf("%c", input[i]);
    					temp[m]=input[i];
    					printf("%c", temp[m]);
    				} //end for 
    					printf("\n");
    
    			strncpy(&bufferarray[b], temp, (strlen(temp)-1));
    			printf("%s\n", bufferarray);
    where i is the index of the input string and m is the index of the temp string. also, the two printfs thrown in there where to check to see if the data copied correctly.

    since some of the inputs are shorter than the rest, the temp string gets copied correctly, but when it is printed, garbage is attached to the end of the string to make it the same length as the first input.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define BSIZE   100
    
    void add_to_buff ( char *buff, char *line ) {
        int     pos;
        char    temp[100];
        sscanf( line, "%d %s", &pos, temp );
        strncpy( &buff[pos], temp, strlen(temp) );
    }
    
    int main ( ) {
        // this is what lines look like when you use fgets to read
        // them from a file
        char    *tests[] = {
    "4 1234\n",
    "22 789\n",
    "8 56\n",
    "16 1234\n",
    "10 789\n",
    "20 56\n",
        };
        char    bufferarray[BSIZE];
        int     i;
    
        for ( i = 0 ; i < BSIZE ; i++ ) bufferarray[i] = 'x';
        bufferarray[BSIZE-1] = '\0';
    
        add_to_buff( bufferarray, tests[0] );
        add_to_buff( bufferarray, tests[1] );
        add_to_buff( bufferarray, tests[2] );
        add_to_buff( bufferarray, tests[3] );
        add_to_buff( bufferarray, tests[4] );
        printf( "%s\n", bufferarray );
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cast to a reference to a static array
    By m37h0d in forum C++ Programming
    Replies: 2
    Last Post: 05-30-2009, 03:40 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  3. Warning while passing array by reference to function
    By Jdo300 in forum C Programming
    Replies: 11
    Last Post: 06-10-2008, 02:40 PM
  4. GCC - Strange networking functions error.
    By maththeorylvr in forum Windows Programming
    Replies: 3
    Last Post: 04-05-2005, 01:00 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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