Silly pointer arithmetic

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
struct ff_file{
	char * cluster_data;
	unsigned int cluster_addr;

	int nr;
	struct directories * start;

	char * free_data;
	char * ptr_to_data;
};

file->ptr_data++;

How do I increment the pointer by one place?

Thus, how do I increment the values pointed to by one place?

This topic has a solution.
Last Edited: Thu. Apr 25, 2019 - 08:21 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

File->ptr_data[0]++;

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Define "one place" ??

 

++ applied to a pointer will move it on by as many bytes are in sizeof(whatever_it_points_to). So:

uint23_t * foo;

foo++;

moves foo by 4 bytes:

typedef struct {
    char data[10];
    uint32_t n;
    uint16_t m;
} str_type;

str_type * bar;

bar++;

moves bar on by 16 bytes etc etc.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

say I have a pointer to type char.

 

struct test{

      char * ptr;

      int other;

} test;

 

value = test->ptr // this reads the char value

 

so does this increment the char value or the pointer by sizeof test or does it increment the value of test by one?

test->ptr++;

 

I suspect it increments the value.  So that leaves the question on how to increment the pointer address by sizeof test? 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

test.ptr is a pointer to char.

Iluvatar is the better part of Valar.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Fianawarrior wrote:

struct test{

      char * ptr;

      int other;

} test;

You make it difficult for us again, just like in the opening post.  Is it even legal to have an instance of "struct test" named "test"?  Perhaps.  But for your question, why make it be ambiguous? 

Fianawarrior wrote:
I suspect it i

And non-syntax-correct [partial] statements in the follow-on discussion.  Please give an example with the value of ahem "value".  Complete examples with numbers please.  No, a value of 4 will not turn into 5.  And it won't increment "test" either.

 

Now you see why FILE cannot be implemented without license consideration.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Thu. Apr 25, 2019 - 02:35 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Fianawarrior wrote:

so does this increment the char value or the pointer by sizeof test or does it increment the value of test by one?

test->ptr++;

 

I guess it depends on precedence? I mean, test->ptr++ is equivalent to (test->ptr)++ or to test->(ptr++)?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I was, like the others, going to say, it's a question of operator precedence then I looked at:

 

https://en.cppreference.com/w/c/language/operator_precedence

 

in which both -> and ++ are "1" so I'd go with El Tangas and sprinkle some liberal () around the place as I can never be arsed to try and work out what the real precedence would be!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
I was, like the others, going to say, it's a question of operator precedence ...

IMO y'all are jumping on that wagon of precedence and related, when OP has posed a bogus question.  Or questions.  See some real compileable code.  Tell what is expected, with values.

 

[My Cousin Vinny]

Trotter: Alright, alright. Now, Miss Vito, being an expert on general automotive knowledge, can you tell me what would be the correct ignition timing be on a 1955 Bellaire Chevrolet with a 327 cubic engine and a 4-barrel carburetor. 

Ms. Vito: It's a bullshit question.

Trotter: Does that mean that you can't answer it?

Ms. Vito: It's a bullshit question. It's impossible to answer.

Trotter: It's impossible because you don't know the answer!

Ms. Vito: Nobody could answer that question!

Trotter: Your Honor, I move to disqualify Miss Vito as a expert witness.

Judge Haller: Can you answer the question?

Ms. Vito: No. It is a trick question.

Judge Haller: Why is it a trick question?

Vincent Gambini: [to his clients] Watch this.

Ms. Vito: 'Cause Chevy didn't make a 327 in '55. The 327 didn't come out til '62. And it wasn't offered in the Bellaire with the 4-barrel carburetor til '64. However, in 1964 the correct ignition timing would be 4 degrees before top dead center.

Trotter: Well, uh, she's acceptable, Your Honor.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A little test program, might help

struct test
{
    char *p;
} test1; /* calling it test is legal but confusing */

char array[] = "help";

int main(void)
{
    struct test *t = &test1;
    t->p = array;

    printf("address %p    value %c same as %c\n", t->p, *t->p, t->p[0]);
    /* increment pointer */
    t->p++;
    printf("address %p    value %c same as %c\n", t->p, *t->p, t->p[0]);
    /* increment what is pointed to */
    (*t->p)++;
    printf("address %p    value %c same as %c\n", t->p, *t->p, t->p[0]);
    /* also increment what is pointed to */
    t->p[0]++;
    printf("address %p    value %c same as %c\n", t->p, *t->p, t->p[0]);

    return 0;
}

produces the following ouptput

address 0x601038    value h same as h
address 0x601039    value e same as e
address 0x601039    value f same as f
address 0x601039    value g same as g