Adding to the address of an array in C

This is probably a very discussed question, but I still fail to understand the mechanics:

How do I add 512 to the address of an array?

Here's the situation:

char buffer[512];
readSector(buffer,2);

/*somewhere in here i must increment address of buffer by 512*/

readSector(buffer,3);
printString(buffer);

readSector takes in the buffer array, and dumps the contents of sector 2 of the disk in it. Then readSector is called again in order to have the contents of sector 3 dropped in buffer. printString just prints the contents of the buffer to the screen.

My professor explains that every time I call readSector I should increment the address of buffer by 512. How do i go about doing this? Again, sorry if this is a blatantly obvious question or if it has been asked before. Thanks!

Upvotes: 0

Views: 1402

Answers (5)

Turns out this question was wrong in its nature. After carefully reading the following instructions:

1. Load the directory sector into a 512 byte character array using readSector
2. Go through the directory trying to match the file name.  If you do not find it, return.
3. Using the sector numbers in the directory, load the file, sector by sector, 
into the buffer array.
You should add 512 to the buffer address every time you call readSector
4. Return

The buffer array is actually a char array initialized in main() which is set to

char buffer[13312];

meaning that by just offsetting I can achieve what I want. Thanks nonetheless everyone!.

Upvotes: 1

user3543009
user3543009

Reputation: 11

If you increment the buffer by 512 -> you will be accessing the array out of the bounds Try using a matrix:

...
char buffer[2][512]; 
readSector(buffer[0],2);
readSector(buffer[1],3);
...

Upvotes: 1

0xF1
0xF1

Reputation: 6116

First of all, you should not try to access the array out of bounds, that is accessing memory region after the last address of array. Doing this is an undefined behavior

i.e. in char buffer[512]; you can only access from *buffer till *(buffer +511).

To read large data in multiples of 512 you can read the first data set in buffer, print it and then overwrite your buffer with next data set if old data set is not needed. Otherwise you can pre-allocate buffer to be of large size (some multiple of 512 e.g. 1024, etc.), then simply access the buffer as buffer for first data set then buffer + 512 for next data set, buffer + 512*2 for third one, and so on.

As others have also pointed out, array name always points to array's base address, you cannot change it, therefore you need to use a dummy pointer say ptr to access any index in array.

char buf[1024];
char *ptr = buf;

readSector(ptr,2); // readSector(buf,2);

//buf = buf + 512; // Wrong, compile error
ptr = ptr + 512; // Correct!!

readSector(ptr,3);
printString(buffer);

Upvotes: 1

m. c.
m. c.

Reputation: 895

Your idea is right, only thing is when you declare

 char buffer[512];

buffer becomes a const type, tat you can not increment any more.

char * p = buffer;

Now this p is the same as buffer, but it is not const type, so you can increment p.

p += 3;

will point to buffer plus 3 location.

Upvotes: 0

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272537

You cannot increment an array. However, you can create a pointer to the first element in the array, and then increment the pointer:

char *ptr = &buffer[0];  // Or just char *ptr = buffer;

ptr += 512;

However, as pointed out below, your array is only 512 bytes. If you increment by 512, you are now looking past the end of the array, which will subsequently invoke undefined behaviour.

Upvotes: 2

Related Questions