Solved

how to change a pointer in function in c

Posted on 2013-06-14
4
479 Views
Last Modified: 2013-06-17
Have a question as follows

#include <stdio.h>
int a(5);                  // pls replace each () with square one. Editor thinks this is html text.

void getAddr(int *addr)
{
  int i;
  addr=&a(2);        //pls replace each () with square one

  for ( i=0; i<5; i++) {
    a(i) = i+10;        //pls replace each () with square one
  }
}

int main()
{
  int i = 0;
  int *p;

  getAddr(p);

  printf("Addr Val\n");
  for ( i=0; i<3; i++)
    printf("%x, %d\n", p+i, *(p+i));

  return 0;
}

There is some error now:

Addr Val
Segmentation fault

Would like to assign the value to pointer p (address) in getAddr (Not in the main). I expect the output is

Addr Val
900998, 12
90099c, 13
9009a0, 14

(Addr column may be different for different computer.)
How to overcome it? Could any gurus shed some light on it?
0
Comment
Question by:jl66
4 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 200 total points
ID: 39249163
int a(8);                  // pls replace each () with square one. Editor thinks this is html text.
int * getAddr()
{
  int i;
  int *addr;
  addr=&a(2);        //pls replace each () with square one

  for ( i=0; i<5; i++) {
    a(i) = i+10;        //pls replace each () with square one
  }
  return addr;
}

int main()
{
  int i = 0;
  int *p;

  p=getAddr();
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 20 total points
ID: 39249239
Side Note:  The EE formatter only cares about [ b ] and [ i ] (no spaces). This line:

a(i) = i+10;        //pls replace each () with square one

...would be the one that the editor would gripe about. If you use the code tags (e.g.
[/code ]), then you won't have that problem  : )

[code]#include <stdio.h>
int a[5];                  // pls replace each () with square one. Editor thinks this is html text.

void getAddr(int *addr)
{
  int i;
  addr=&a[2];        //pls replace each () with square one

  for ( i=0; i<5; i++) {
    a[i] = i+10;        //pls replace each () with square one
  } 
}

int main()
{
  int i = 0;
  int *p;

  getAddr(p);

  printf("Addr Val\n");
  for ( i=0; i<3; i++)
    printf("%x, %d\n", p+i, *(p+i));

  return 0;
}

Open in new window

0
 
LVL 6

Accepted Solution

by:
limweizhong earned 280 total points
ID: 39250153
In C++ you could pass the pointer variable in by reference (using the variable inside actually changes the variable passed in), by putting a "&" like so:
void getAddr(int *&addr)

Open in new window


However, there is also the C-style method, which actually shows you what is being done:
#include <stdio.h>
int a[5];                  // pls replace each () with square one. Editor thinks this is html text.

void getAddr(int **addr)
{
  int i;
  *addr=&a[2];        //pls replace each () with square one

  for ( i=0; i<5; i++) {
    a[i] = i+10;        //pls replace each () with square one
  } 
}

int main()
{
  int i = 0;
  int *p;

  getAddr(&p);

  printf("Addr Val\n");
  for ( i=0; i<3; i++)
    printf("%x, %d\n", p+i, *(p+i));

  return 0;
}

Open in new window

Notice that I changed lines 4, 7 and 19.
0
 

Author Closing Comment

by:jl66
ID: 39253308
Thank all of you so much.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now