Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 334
  • Last Modified:

Simple Quick Easy Pointers

Hi,

I have some course work in for tomorrow and just cannot get my head around pointers.  I only need a simple one.

As far as i am aware a pointer can be used to pass data to anf from for a function becuase its a location in memory.  This is all i want ....

#include <stdio.h>

void test(int *ptr);

void main(void)
{
  int *ptr;
  ptr = 4;
  printf("%d", &ptr);
  test(*ptr);
  printf("%d", &ptr);
  getch();
}

void test(int *ptr)
{
  ptr = 7;
}

Please i have tried to simplify this code and i am running out of time quickly.  I need something like the above example to work asap.  It also has to be using pointers no return statement or anything.

Cheers,

Daz
0
yo_daz_uk
Asked:
yo_daz_uk
  • 6
  • 5
  • 4
  • +5
1 Solution
 
griesshCommented:
Hi yo_daz_uk,

We are not allowed to do your work, that would be against the Member Agreement. But we can help you understand your problem:

"int *ptr" allocates space for a pointer to an integer, ptr holds the address of the memory cell that holds an int data type!
=> ptr = 4 says there is a int at memory address 4
=> &ptr is a pointer to the pointer to an int ...
=> test (*ptr) uses the content of the memory cell that pointer points to,
I hope

======
Werner
0
 
hal3000Commented:
Hi yo_daz_uk,

You are on the right track with what you have in code. Just remember that if you wish to work with the value pointed to by a pointer you must dereference that pointer and not change the pointer itself.

Good luck
0
 
dimitryCommented:
Pointer "points" to memory location.
But you need to allocate it.
So use:
  int myvar;
  int *ptr;

  ptr = &myvar;

Also remember that to access this memory you need to use '*' and not '&':
 *ptr = 7;

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
griesshCommented:
&x gives you the ADDRESS of the memory where your data is stored
*y gives you the DATA pointer y is pointing to
0
 
yo_daz_ukAuthor Commented:
Thanks for your replies ....

I have altered the code slightly

#include <stdio.h>
#include <conio.h>

void test(int *ptr);

void main(void)
{
  int *ptr, x=4;
  ptr = &x;


  printf("%d", &ptr);
  test(ptr);
  printf("%d", &ptr);
  getch();
}

void test(int *ptr)
{
  int x=5;
  ptr = &x;
}

It compiled with a warning saying that ptr is never used in function test.  Also when i ran it afterwards it wasnt showing 4 and 5 as expected.  It was showing -12 any ideas?

Daz
0
 
Avik DasguptaCommented:
>>printf("%d", &ptr);
ptr is the pointer itself
printf("%d",*ptr);

Avik.
0
 
yo_daz_ukAuthor Commented:
ok ive done the printf("%d",*ptr); bit, but it is still not showing the correct number, it shows somet like -2817-2817.
0
 
Avik DasguptaCommented:
try writing the function as
int * test(int *ptr); because u need to get back the pointer value that was changed within the funtion.
Modify the function.
int * test(int *ptr)
{
  int x=5;
  ptr = &x;
  return ptr;
}
and accept the value as
ptr=test(ptr);

Combine my two posts and ur answer is there.

Avik.
0
 
ankuratvbCommented:
Also,
void test(int *ptr)
{
 int x=5;
 ptr = &x;
}

int x=5;

is a local variable so it goes out of scope the moment the function ends,so the address now held in ptr may noe be valid.

0
 
Avik DasguptaCommented:
Well uv tried so far a good effort. I have rewritten ur program .
#include <stdio.h>
#include <conio.h>

int * test(int *ptr);

void main(void)
{
  int *ptr, x=4;
  ptr = &x;
  clrscr();

  printf("%d\n", *ptr);
  ptr=test(ptr);
  printf("%d", *ptr);
  getch();
}

int * test(int *ptr)
{
  int x=5;
  ptr = &x;
  return ptr;
}

It's compiling in Turbo C++ v3.0 ok and giving proper results as expected.
Good luck.
Avik.
0
 
griesshCommented:
Avik's test() is wrong for reasons ankuratvb explained already.

What you can learn from that: only trust your own solution once you understand it!

=====
Werner
0
 
yo_daz_ukAuthor Commented:
ok well i think the local variable thing might be right, becuase strangely with the same code i now get 44.

However, even if x is local it is initialised again as a local variable, so i presumed when it was passed to ptr, and that was given back that would hold the old x value?

The reason i wanted to keep away from the return is because i will neeed to pass back more than one variable in one of my functions, and i thing the return value can only return back one value cant it?

I dont have to use ptr, i thought this is what i had to use to pass to variables to a function and reurn 2 variables, is this not correct?

0
 
yo_daz_ukAuthor Commented:
:-( sorry about the poor spelling and grammer.  Typing too quickly.
0
 
Avik DasguptaCommented:
>>Avik's test() is wrong for reasons ankuratvb explained already.
ptr is a local variable to the function and the pointer value assigned to it as ptr=&x is returned to main where it is being captured by the ptr variable in main().
Well griessh,
   my compiler has understood it and responding 4 5.
Avik.
0
 
Avik DasguptaCommented:
If u wan't to use the x u used in main() then
void test(int *n);

void test(int *x)
{
  *x=5;
}

and call as
test(&x);

Avik.

0
 
PerryDKCommented:
Output is :
4
7

Maybe I'm doing too much but I find it hard to believe that this would be an entire assignment rather it sounds like a piece to a bigger assignment.  At anyrate I think this what you want.  I have gave descriptions of all of your errors.  If you need further descriptions please ask...ESPECIALLY if this trully is your entire assignment.


#include <stdio.h>

void test(int *ptr);

void main(void)
{
  int *ptr;

  //YOU HAD
  //ptr = 4;

  //you can not and should not assign anthing to ptr other than
  //a memory adress.  You can either assign the address of an already
  //defined integer or you can use the new operator if you are using c++
  //and not strictly defined to c.  If you strictly defined to see you will
  //have to use malloc.  As this really doesn't pertain to your
  //question I won't disucsss new or malloc any further.

  //YOU SHOULD HAVE
  int myInt = 4;
  ptr = &myInt;

  //YOU HAD
  //printf("%d", &ptr);
  //what you are printing here is the address of the pointer.
  //in other words your printing the address of the pointer that is pointing
  //to the adress of integer.  A Pointer to a pointer.
  //not what you want you want to be printing.
  //You probably want to be printing  the value of what ptr is pointing to
  //So you need to derefence the pointer to get the value

  //YOU SHOULD HAVE
  printf("%d\n", *ptr);


  //YOU HAD
  //test(*ptr);

  //You now are dereferencing the memory address and it is converted
  //from an address into an actual int
  //But your function takes a pointer to an int not an int

  //YOU SHOULD HAVE
  test(ptr);

  //YOU HAD
  //printf("%d", &ptr);

  //again you are printing the address not the value
  //YOU SHOULD HAVE
  printf("%d\n", *ptr);
  //getch();
}

void test(int *ptr)
{
  //YOU HAD
  //ptr = 7;

  //Again you can not assign (well SHOULD NOT) assign anything to an int*
  //other than a memory address that points to an integer.
  //To change the value of what ptr points to to the value 7
  //you first need to deference the pointer and then assign the value 7

  //YOU SHOULD HAVE
  *ptr = 7;
}
0
 
Avik DasguptaCommented:
//Hence it stands to this ...

#include <stdio.h>
#include <conio.h>

void test(int *ptr);

void main(void)
{
  int *ptr, x=4;
  ptr = &x;
  clrscr();

  printf("%d\n", *ptr);
  test(&x);
  printf("%d", *ptr);
  getch();
}

void test(int *x)
{
  *x=5;
}

Avik.
0
 
yo_daz_ukAuthor Commented:
Avik,

your code was spot on ....... PeeryDK i thought i owed it to Avik to look at his first as he had sent a few replies, before and it works.  Sorry, but thanks for your reply anyway.

Thanks guys that works great ... i thought i made it clear from the start this was not my whole assignment.  In fact its probably less worth than 1% of the value ... its just i couldnt work this part out.

Thanks a lot again!!!
0
 
griesshCommented:
>> my compiler has understood it and responding 4 5.

<LOL> that doesn't make it right
0
 
PerryDKCommented:
you could have split the points :)...trying to maintain my status as an expert and receive free status :)

As long as you learned from my comments I guess thats all that counts.
0
 
stefan73Commented:
Hi yo_daz_uk,

ALWAYS compile your code with full compiler warnings enabled.

You could have spotted a solution to your problems in the first place by proper compiler warnings.

Cheers,
Stefan
0
 
ankuratvbCommented:
>> my compiler has understood it and responding 4 5.
>><LOL> that doesn't make it right

Exactly.
See my statement:
>the address now held in ptr *may* not be valid.

Since C doesnt have any concept of garbage collection,it doesnt clear the values of deallocated variables,so its entirely possible that on a particular run of your program(or for that matter,multiple runs),that u get the right value,but you are accessing a memory location which points to a variable that is no longer allocated,which *might* give you the wrong value.

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
  • 4
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now