Go Premium for a chance to win a PS4. Enter to Win

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

how to change a pointer in function in c

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
jl66
Asked:
jl66
3 Solutions
 
ozoCommented:
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
 
käµfm³d 👽Commented:
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
 
limweizhongCommented:
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
 
jl66Author Commented:
Thank all of you so much.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now