Solved

how to change a pointer in function in c

Posted on 2013-06-14
4
498 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 conditional statements in the C programming language.

624 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