Jump to content

decrement pointer's value

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
random guy

random guy

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
hi i have a list of integers (the list class is custom made by nachos)

i can remove an integer with

aWaitTime=(int*) aController->aListOfWaitTimesforLargeAirstrips->Remove();

so the type is int*

but i dont think i can do

aWaitTime->aWaitTime-1;

to decrement the value being pointed at. so for example if the pointer was pointing to a value of 30 i want the value to become 29.

i tried to do this

aWaitTime->aWaitTime-(int*)1;

but that threw an error.

any ideas?

Edited by Jaan, 21 September 2009 - 08:42 AM.
Please use code tags when you are posting your codes !

Hey like something i said? Helped you out? Or you just like supporting the Random Guy?
add to my rep. its quick and easy and definitely wont steal your girlfriend.

#2
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
You're not assigning a value to the variable. Please read through this page (and I'd recommend reading through the whole tutorial) and you should understand what you were doing and how to fix it.

Operators

#3
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

random guy said:

hi i have a list of integers (the list class is custom made by nachos)

i can remove an integer with

aWaitTime=(int*) aController->aListOfWaitTimesforLargeAirstrips->Remove();

so the type is int*

but i dont think i can do

aWaitTime->aWaitTime-1;

to decrement the value being pointed at. so for example if the pointer was pointing to a value of 30 i want the value to become 29.

i tried to do this

aWaitTime->aWaitTime-(int*)1;

but that threw an error.

any ideas?
Any time I see casts, I wonder if they're necessary. Are they? (The second one in particular makes no sense.)

As mentioned, this code doesn't do anything -- it's sort of a no-op.
aWaitTime->aWaitTime-1;
If aWaitTime is an int*, you can decrement it many ways:
   --aWaitTime;

   aWaitTime--;

   aWaitTime -= 1;

   aWaitTime = aWaitTime - 1;


#4
random guy

random guy

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
i have a place where it is
aWaitTime=(int*) aController->aListOfWaitTimesforLargeAirstrips->Remove();
printf("value is %d\n",aWaitTime);
aWaitTime=aWaitTime-1;
printf("value is %d\n",aWaitTime);
the first printf statement prints 0 (which is what it started at, this is correct)
and the second one printed out -4 (wrong, we decremented by 1)

also once that happens if i do
if (aWaitTime <= 0)
   printf("Entered the if statement");
it doesnt enter even though the value was shown as -4.

NOTE: if i do awaittime=(int*) 20; the value is changed to 20. (or at least it the printf is printing out 20)

Edited by Jaan, 21 September 2009 - 08:43 AM.
Please use code tags when you are posting your codes !

Hey like something i said? Helped you out? Or you just like supporting the Random Guy?
add to my rep. its quick and easy and definitely wont steal your girlfriend.

#5
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
I'm assuming aWaitTime is an integer pointer, and that aController->aListOf...Airstrips->Remove(); returns a pointer to an integer, right?

Anyway, if that's the case, you are decrementing the pointer, all that's happening is that the int value pointed to by the address prior to wherever "Remove()" returned is -4, not -1. To decrement the value that aWaitTime holds, you do *aWaitTime -= 1;. What does Remove() return?
Wow I changed my sig!

#6
random guy

random guy

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
remove returns a pointer to an integer. it is actually a void* which is why it has to be casted as an int* here:

aWaitTime=(int*) aController->aListOfWaitTimesforLargeAirstrips->Remove();

aWaitTimes is an int*

i tried *aWaitTime-=1; but that produces a segfault.

Edited by Jaan, 21 September 2009 - 08:44 AM.
Please use code tags when you are posting your codes !

Hey like something i said? Helped you out? Or you just like supporting the Random Guy?
add to my rep. its quick and easy and definitely wont steal your girlfriend.

#7
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
So the Remove() function returns a null pointer and you attempt to do things with it? I'd expect a crash.

Going from 0 to -4 would be expected in this case if it wasn't undefined behavior to begin with.

#8
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
I haven't used NachOS in a very long time, so this may be wrong :3

#include "list.h"

...
int a = 1;
int *b = NULL;
...
mylist.Append((void*)&a); //Adds an entry to the end of the list
...
b = (int*)mylist.Remove(); //Removes an entry from the end of a list

if(b == NULL){ //Remove() returns NULL if the list was empty
    printf("EMPTY LIST!\n");
    return 1;
}else{
    *b--;
    printf("Value of b: %d\n",*b);
}


#9
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
If you want to delete the value pointed to by a pointer, use the * operator to dereference the pointer:
#include <stdio.h>


int main()

{

   int array[] = {10,20,30,40,50,60,70,80,90};

   int *aWaitTime = &array[sizeof array / sizeof *array - 1];


   [COLOR="darkgray"]printf("aWaitTime = %p, *aWaitTime = %d\n", (void*)aWaitTime, *aWaitTime);[/COLOR]

   --aWaitTime;

   [COLOR="darkgray"]printf("aWaitTime = %p, *aWaitTime = %d\n", (void*)aWaitTime, *aWaitTime);[/COLOR]

   aWaitTime--;

   [COLOR="darkgray"]printf("aWaitTime = %p, *aWaitTime = %d\n", (void*)aWaitTime, *aWaitTime);[/COLOR]

   aWaitTime -= 1;

   [COLOR="darkgray"]printf("aWaitTime = %p, *aWaitTime = %d\n", (void*)aWaitTime, *aWaitTime);[/COLOR]

   aWaitTime = aWaitTime - 1;

   [COLOR="darkgray"]printf("aWaitTime = %p, *aWaitTime = %d\n", (void*)aWaitTime, *aWaitTime);[/COLOR]


   [COLOR="darkgray"]puts("---");[/COLOR]


   [COLOR="red"]--*aWaitTime;[/COLOR]

   [COLOR="darkgray"]printf("aWaitTime = %p, *aWaitTime = %d\n", (void*)aWaitTime, *aWaitTime);[/COLOR]

   [COLOR="red"](*aWaitTime)--;[/COLOR]

   [COLOR="darkgray"]printf("aWaitTime = %p, *aWaitTime = %d\n", (void*)aWaitTime, *aWaitTime);[/COLOR]

   [COLOR="red"]*aWaitTime -= 1;[/COLOR]

   [COLOR="darkgray"]printf("aWaitTime = %p, *aWaitTime = %d\n", (void*)aWaitTime, *aWaitTime);[/COLOR]

   [COLOR="red"]*aWaitTime = *aWaitTime - 1;[/COLOR]

   [COLOR="darkgray"]printf("aWaitTime = %p, *aWaitTime = %d\n", (void*)aWaitTime, *aWaitTime);

[/COLOR]

   return 0;

}


/* my output

aWaitTime = 0022FF40, *aWaitTime = 90

aWaitTime = 0022FF3C, *aWaitTime = 80

aWaitTime = 0022FF38, *aWaitTime = 70

aWaitTime = 0022FF34, *aWaitTime = 60

aWaitTime = 0022FF30, *aWaitTime = 50

---

aWaitTime = 0022FF30, *aWaitTime = 49

aWaitTime = 0022FF30, *aWaitTime = 48

aWaitTime = 0022FF30, *aWaitTime = 47

aWaitTime = 0022FF30, *aWaitTime = 46

*/

But first make sure you don't have a null pointer.

#10
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
@dcs: Neener-Neener, beat you to it :D

He's using the list class from NachOS's threading implementation, but thats still a good pointer example. +rep.