1

I am trying to get access the buffer called buf from function readfile. When I print sizeof(buf) I see that buf has 4 bytes (pointer). On the other hand, if I paste the printf command on the readFiles, I can see that the size is 2916. Actually, I don't understand why it is not 729, but it is obvious I cannot access the buf inside the readfile which I need. So the question is; where is the problem and how to correct it?

void readfiles(FILES * files){
    unsigned char * buf [1*729];
    int skip_lines = 14;
    int shift = 0;
    char * filename = "file.txt";
    // printf("buf size %d", sizeof(buf));
    readfile(filename, skip_lines, buf, shift);
}
int readfile(char * name, int skip, unsigned char * buf, int shift ){
 // buf is (unsigned char *) on address 0x22dd18 ""
    printf("buf size %d", sizeof(buf));
}
7
  • 5
    Just change unsigned char * buf [1*729]; to unsigned char buf [1*729]; and suddenly your problem will go away (well, at least go elsewhere) Commented Jul 31, 2017 at 16:30
  • 3
    buf is four bytes and 4 * 729 = 2916. You declared an array of pointers. Commented Jul 31, 2017 at 16:30
  • Removing pointer solves one problem (size is 729 now). But when I removed the pointer I still cannot access the buf (size is 4 still). Also when I have removed the pointer will the data be saved in the buffer? Commented Jul 31, 2017 at 16:39
  • Which line prints the 4 bytes? Commented Jul 31, 2017 at 16:44
  • to print sizeof use %zu Commented Jul 31, 2017 at 16:44

2 Answers 2

5

if you pass an array as a pointer into a C function you cannot detect its length.

int readfile(char * name, int skip, unsigned char * buf, int shift ){
// nothing you do in here can tell how big buf is
}

if you need to know the length of buf you have to pass it in as a paramter

int readfile(char * name, int skip, unsigned char * buf,int blen, int shift ){
...
}
Sign up to request clarification or add additional context in comments.

Comments

0

In readfile(),

printf("buf size %d", sizeof(buf));

This prints 4 because buf is a pointer, as you seem to understand. The same print statement in readfiles(). prints a larger number because there buf is an array.

As pm100 stated, you cannot get the size of an array from a pointer to that array. Additionally, you need to be very careful about accessing pointers. If they do not point at a valid memory address, then you will have a lot of bugs. These are difficult to track down because the problems will occur in other parts of your code than where the pointer errors are. I suggest you learn about malloc() and free() in order to dynamically allocate memory for your pointers.

2 Comments

I know using malloc, do you think I should use it here instead of unsigned char array?
@user1141649 I do not think there is any "instead" here. The array of unsigned char in readfiles() seems appropriate. However, in readfiles() you have an array of pointers. Those need to point at some valid memory address or you will encounter problems. Or maybe this is a mistake and should be an array of unsigned char instead.

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.