2

I am having the following problem. I have created a char array which represents a series of characters and numbers - this was designed to model a string read or got from a text file. I wish to then search this string using the "search" function defined below, pulling out only the numbers before 'H' and assigning to a separately defined integer array. I find when I use gdb, this function works fine. However, only part of the array is ever returned - the first 8 elements to be exact. Would anyone please be able to explain why this is from looking at the code below?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void search(char buffer[], int size, int array[]);

int main (void)
{
    char buffer[1000];
    memset(buffer, 0, sizeof(buffer)); 

    buffer[0] = 2;
    buffer[1] = 'H';
    buffer[2] = 3;
    buffer[3] = 'H';  
    buffer[4] = 6;
    buffer[5] = 'H'; 
    buffer[6] = 4; 
    buffer[7] = 'H'; 
    buffer[8] = 6; 
    buffer[9] = 'H';
    buffer[10] = 7;
    buffer[11] = 'H';
    buffer[12] = 11;
    buffer[13] = 'H';
    buffer[14] = 12;
    buffer[15] = 'H';
    buffer[16] = 17;
    buffer[17] = 'H'; 

    int* array ;    
    array = malloc(sizeof(buffer) * sizeof(int));  

    search(buffer, sizeof(buffer), array); 

    for(int i = 0, n = sizeof(array); i < n; i++)
    {
        printf("array[%d] = %d\n", i, array[i]);
    } 

    free(array); 
}

void search(char buffer[], int size, int array[])
{
    int position = 0;

    for (int i = 0; i < size; i++)
    {
        if(buffer[i] == 'H')
        {
            *(array + position) = buffer[i-1];
            position++;
        }
    }       
}

The compiler outputs the following:

array[0] = 2
array[1] = 3
array[2] = 6
array[3] = 4
array[4] = 6
array[5] = 7
array[6] = 11
array[7] = 12

which as can be seen is missing the ninth position in the array - value 17. In fact, if I fgets into a buffer a much bigger set of numbers and 'H's, I am always returned an array of size 8. Why is this? Any help would be much appreciated.

7
  • You are passing sizeof(buffer) to search(), NOT the number of elements you have used - 18. Commented Aug 21, 2015 at 22:30
  • buffer[i-1]; is Undefined Behaviour when i == 0 Commented Aug 21, 2015 at 22:31
  • @WeatherVane sizeof(buffer) means number of elements of the array. buffer[i-1]; if (buffer[i] == 'H') Commented Aug 21, 2015 at 22:36
  • @BLUEPIXY please read my comments more carefully. Commented Aug 21, 2015 at 22:40
  • @WeatherVane You do not need to pass the number of elements that you are using. when i == 0, will not be access to the buffer[i-1]. Commented Aug 21, 2015 at 22:41

2 Answers 2

2

The problem here is with

n = sizeof(array)

array being a pointer, sizeof(array) will give you the size of the pointer (as sizeof(array) here is essentially sizeof(int *), which seems to be 8 for your platform), not the cumulative size (or number of elements) in the array.

One possible way to get what you want will be the search() to return the count of element put into the array and use that return value in the for loop for printing the required elements from array.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi thanks, you are absolutely right and this worked a treat! Cheers!
1

Replace n = sizeof(array) with n = strlen(buffer) / 2 will do (you want n to be the number of filled elements in the array).

Btw,

  • You need to make sure the buffer always has correct pairs.
  • Would be safer to declare the buffer like this char buffer[1000] = "";
  • "I am always returned an array of size 8" - that's because sizeof(int) in 64b-machine is 8 bytes, which is what you got from sizeof(array) - as array here is pointer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.