Solved

how to change a pointer in function in c

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

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-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.

762 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

18 Experts available now in Live!

Get 1:1 Help Now