473,386 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

hi , i'm having a problem with pointers in C ...

I will give a simple example inspired from my problem :

void change(int *x, int *y){x=y;}

int main(){
int *xx,*yy;

yy = (int*)malloc(sizeof(int));
change(xx,yy);

if(xx == NULL)printf("is null\n");
else printf("---- %d ----\n",*xx);

.................................................. .........

xx doesn't point to the value of yy (*yy) , the program prints "is null" . Why ?

xx and yy must be declared int* , it's something that i must do in my program .
Oct 25 '08 #1
7 1325
donbock
2,426 Expert 2GB
Is there some reason why couldn't replace "change(xx,yy);" with "xx = yy;" ?

Take a closer look at the change() function. You're trying to copy the value of one pointer argument into another pointer argument.
Expand|Select|Wrap|Line Numbers
  1. void change(int *x, int *y) {
  2.    x = y;
  3. }
Consider a similar function that seeks to copy the value of one integer argument into another integer argument. All I've done is change the data type of the arguments, not the basic mission of the function.
Expand|Select|Wrap|Line Numbers
  1. void changeInt(int x, int y) {
  2.    x = y;
  3. }
Why doesn't changeInt() work? It is the same reason that change() doesn't work.
Oct 25 '08 #2
then how can i copy the value of one pointer argument into another pointer argument ?

xx = yy must be done in a function . the original program is much complex and it has to be done that way .
Oct 25 '08 #3
donbock
2,426 Expert 2GB
then how can i copy the value of one pointer argument into another pointer argument ?
How would you copy the value of one integer argument into another integer argument?
Oct 25 '08 #4
How would you copy the value of one integer argument into another integer argument?

change(int*x , int* y){*x=*y;}

main(){
int x,y;
y=7;
change(&x,&y);
}

to be similar to the one with pointers , i would do it this way
Oct 25 '08 #5
How would you copy the value of one integer argument into another integer argument?

got it 10x , correct me if i'm wrong .. .....

void change(int **x, int **y){*x=*y;}

int main(){
int *xx,*yy;

yy = (int*)malloc(sizeof(int));
change(&xx,&yy);

if(xx == NULL)printf("is null\n");
else printf("---- %d ----\n",*xx);
Oct 25 '08 #6
donbock
2,426 Expert 2GB
That should work. Did you get the right answer?

I would recommend the following variation:
Expand|Select|Wrap|Line Numbers
  1. void change (int **xx, int *yy) {
  2.    *xx = yy;
  3. }
Or more generally, for any type T:
Expand|Select|Wrap|Line Numbers
  1. void change (T *out, T in) {
  2.    *out = in;
  3. }
The difference lies in not using a pointer for the input value. That reduces the chances that through some typo you might inadvertently change the input value. Sometimes you can't avoid using a pointer for the input, such as when the input is a structure.

Another variation is
Expand|Select|Wrap|Line Numbers
  1. int *change(int *yy) {
  2.    return yy;
  3. }
However, I'd advise not using the return value to copy a structure. The C Standard tells you that a function can indeed return a structure; but what it doesn't tell you is that some compilers implement that feature in a way that isn't thread safe. At this point in your C experience you're probably not worried about thread-safety, but it is never too early to learn good habits.
Oct 26 '08 #7
donbock
2,426 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. yy = (int*)malloc(sizeof(int));
  2. change(&xx,&yy);
  3. if(xx == NULL)printf("is null\n");
  4. else printf("---- %d ----\n",*xx);
Um, you have a different problem here. Now that xx isn't NULL you'll take the "else" branch. What's going to happen when you dereference xx in the printf argument list?
Oct 26 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Joseph | last post by:
I was doing my assignment,but encountered a problem at last step!!!!!! for easy reading, i ommited lots of other things //=====================code begin================================...
16
by: yuraukar | last post by:
I am trying to create a garbage collection class in C++ to collect instances of a specific class (so no general gc). The approach is to use smart pointers and a mark and a simple sweep gc. ...
14
by: Gregory L. Hansen | last post by:
I can't seem to make a queue of objects, using the STL queue. I'm trying to make a little event manager, and I just want someplace to store events. The method definitions for EventManager have...
3
by: Markus Dehmann | last post by:
I have a class "Data" and I store Data pointers in an STL set. But I have millions of inserts and many more lookups, and my profiler found that they cost a lot of runtime. Therefore, I want to...
7
by: | last post by:
I fail to understand why that the memory allocated in the void create(int **matrix) does not remain. I passed the address of matrix so shouldn't it still have the allocated memory when it returns...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
8
by: Vmz | last post by:
HI, I got two queues, I need to write void function that would check both queues and remove from first queue all nodes that matches second queue. Each node has two pointers that shows to previous...
17
by: Chad | last post by:
I'm want static char *output; to hold the modified string "tel chad" However, when I debug it, static char *output holds the ascii value of the strng, and not the string itself. Here is...
9
by: David | last post by:
Hi all, I posted my question two days ago, and tried to solve this problem. but until now I didn't solve that. and I cut my codes so maybe this time it is more readable. ...
38
by: sam_cit | last post by:
Hi, I have the following Struct, Struct Sample { int i; char *p; };
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.