Solved

how to change a pointer in function in c

Posted on 2013-06-14
4
488 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

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!

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…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

685 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