Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wanted to make a function that takes a pointer to integer as a parameter, and sets the value to 0 in c programming language.But I do not know very well about its algorithm and how it works .I tried to solve it But I could not.
Can you help me and please explain some of your codes or suggestions that you will give me . I started learning very few times ago .I hope you will not ignore my request

What I have tried:

void my_initializer(int* param_1)
{
int anti = 0 ;
my_initializer(&anti);

}
Posted
Updated 1-Aug-22 0:12am

void my_initializer(int* param_1)
{
   // Dereferencing the pointer to assign the value
   *param_1= 0;
}

int main()
{
  int anti;
  // Pass the address of variable anti
  my_initializer(&anti);
  return(0);
}


I hope it helps.
 
Share this answer
 
To understand what 0x01AA has written, you need to understand what a pointer is and what an address is - so lets start with a real-world example: a book.

A book is made of pages which are covered with lines of words called "text". Each page has a number which starts at 1 with the first page, and is incremented by one for each subsequent page.
But that text is organised into chapters which often have names which describe what goes on in the chapter. For "Winnie The Pooh" by A.A. Milne, the chapters are
In Which We Are Introduced to Winnie-the-Pooh and Some Bees and the Stories Begin
In Which Pooh Goes Visiting and Gets Into a Tight Place
In Which Pooh and Piglet Go Hunting and Nearly Catch a Woozle
In Which Eeyore Loses A Tail and Pooh Finds One
In Which Piglet Meets a Heffalump
In Which Eeyore Has A Birthday And Gets Two Presents
In Which Kanga And Baby Roo Come To The Forest And Piglet Has A Bath
In Which Christopher Robin Leads An Expedition To The North Pole
In Which Piglet Is Entirely Surrounded By Water
In Which Christopher Robin Gives Pooh A Party and We Say Goodbye


Near the front of the book, you often find a Contents page which lists all the chapters together with the number of the page that chapter starts on (which will vary by edition because the page size (paperbacks are physically smaller than hardback editions), font size, font selection, and image placement all affect the number of pages in the printed edition.

If you want to read the chapter "In Which Piglet Meets a Heffalump" you need to either read every page until you find it, or look at the Contents find the chapter and look up the page number:

In Which We Are Introduced to Winnie-the-Pooh and Some Bees and the Stories Begin       6
In Which Pooh Goes Visiting and Gets Into a Tight Place                                     25
In Which Pooh and Piglet Go Hunting and Nearly Catch a Woozle                               57
In Which Eeyore Loses A Tail and Pooh Finds One                                             83
In Which Piglet Meets a Heffalump                                                          102
In Which Eeyore Has A Birthday And Gets Two Presents                                       145
In Which Kanga And Baby Roo Come To The Forest And Piglet Has A Bath                       178
In Which Christopher Robin Leads An Expedition To The North Pole                           201
In Which Piglet Is Entirely Surrounded By Water                                            234
In Which Christopher Robin Gives Pooh A Party and We Say Goodbye                           269

If we do that, we get "102" so we can go directly to page 102 and start reading about Heffalumps (always worth doing!)
The page number points up directly to the page we want, and as the edition changes, so does the page number to it always points us at the right page, regardless of how many pages there are in the book.

In computing terms, the Contents page is full of "ChapterN" variables (probably an array of Chapter structs) which hold the chapter title and a pointer to the page - the number associated with each page in the book is a unique address that tells us where that page goes.

You can pass an address into a function by declaring the function to take a pointer-type parameter:
void my_initializer(int* param_1)
{
   // Dereferencing the pointer to assign the value
   *param_1= 0;
}
param_1 is declared as a "pointer to an integer" by the type that prefixes it: int*
All you need now is to pass an address to the function when you call it, which you do by using the Address Of operator (an & character before the variable name).:
int anti;
// Pass the address of variable anti
my_initializer(&anti);
Make sense now?
 
Share this answer
 
v2
Comments
Richard MacCutchan 30-Jul-22 10:37am    
I am glad to see that you have at least one useful book in your library. :)
OriginalGriff 30-Jul-22 11:03am    
One of the essentials, I believe! :D
0x01AA 30-Jul-22 13:17pm    
A 5.
But: 'which you do by prefixing a variable name by an & character' is better called 'using the Address Of Operator' '&'. I mean it is not a prefix it is an operator ;)

I hope I'm not too nitpicking :D
OriginalGriff 30-Jul-22 14:12pm    
Much better - modified! :thumbsup:
0x01AA 30-Jul-22 14:31pm    
Thank you Sir
void my_initializer(int* pram)
{
*pram = 0;


}
 
Share this answer
 
Comments
CHill60 1-Aug-22 8:43am    
An unformatted, uncommented code dump is not a solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900