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));
}
John Boe
  • 3,501
  • 10
  • 37
  • 71
  • 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) – Eugene Sh. Jul 31 '17 at 16:30
  • 3
    buf is four bytes and 4 * 729 = 2916. You declared an array of pointers. – TomServo Jul 31 '17 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? – John Boe Jul 31 '17 at 16:39
  • Which line prints the 4 bytes? – Code-Apprentice Jul 31 '17 at 16:44
  • [to print `sizeof` use `%zu`](https://stackoverflow.com/q/940087/995714) – phuclv Jul 31 '17 at 16:44
  • I am a little confused by your question. You have two printf() calls, one of which is commented out. Furthermore, you print a variable named `buf` for both, but each `buf` has a different type. Which of these `printf()`s are you asking about? – Code-Apprentice Jul 31 '17 at 16:47
  • The commented buf is that which had size 2916 when I have tested is uncommented. Then I have commented it out and tested the second printf which is in the function readfile. This one shows pointer size 4. – John Boe Jul 31 '17 at 17:02

2 Answers2

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 ){
...
}
pm100
  • 48,078
  • 23
  • 82
  • 145
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.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • I know using malloc, do you think I should use it here instead of unsigned char array? – John Boe Jul 31 '17 at 17:04
  • @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. – Code-Apprentice Jul 31 '17 at 19:00