Solved

Simple Quick Easy Pointers

Posted on 2004-04-27
22
317 Views
Last Modified: 2010-04-02
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
Comment
Question by:yo_daz_uk
  • 6
  • 5
  • 4
  • +5
22 Comments
 
LVL 11

Expert Comment

by:griessh
ID: 10931906
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
 
LVL 1

Expert Comment

by:hal3000
ID: 10931922
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
 
LVL 11

Expert Comment

by:dimitry
ID: 10931929
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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 11

Expert Comment

by:griessh
ID: 10931934
&x gives you the ADDRESS of the memory where your data is stored
*y gives you the DATA pointer y is pointing to
0
 

Author Comment

by:yo_daz_uk
ID: 10932138
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
 
LVL 2

Expert Comment

by:Avik77
ID: 10932215
>>printf("%d", &ptr);
ptr is the pointer itself
printf("%d",*ptr);

Avik.
0
 

Author Comment

by:yo_daz_uk
ID: 10932283
ok ive done the printf("%d",*ptr); bit, but it is still not showing the correct number, it shows somet like -2817-2817.
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10932305
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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10932312
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
 
LVL 2

Expert Comment

by:Avik77
ID: 10932352
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
 
LVL 11

Expert Comment

by:griessh
ID: 10932408
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
 

Author Comment

by:yo_daz_uk
ID: 10932422
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
 

Author Comment

by:yo_daz_uk
ID: 10932448
:-( sorry about the poor spelling and grammer.  Typing too quickly.
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10932572
>>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
 
LVL 2

Expert Comment

by:Avik77
ID: 10932631
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
 
LVL 4

Expert Comment

by:PerryDK
ID: 10932642
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
 
LVL 2

Accepted Solution

by:
Avik77 earned 95 total points
ID: 10932655
//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
 

Author Comment

by:yo_daz_uk
ID: 10932795
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
 
LVL 11

Expert Comment

by:griessh
ID: 10934958
>> my compiler has understood it and responding 4 5.

<LOL> that doesn't make it right
0
 
LVL 4

Expert Comment

by:PerryDK
ID: 10935904
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
 
LVL 12

Expert Comment

by:stefan73
ID: 10936817
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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10938123
>> 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
convert char array to number in c 5 86
Digital Output does not have continuity to ground? 3 100
change colour of repeater control in asp.net c# 7 113
Unable to start eclipse ? 17 153
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question