C column of Pointer

Now , let us go on talking about the pointer , in the "C column of Pointer <0>" had detailed the definition ,initialization and access the pointer . Most of us maybe have been  clear about the  the memory allocation.  Here we go 

Here is the definition

#include <stdio.h>
int main(void)
{
  int price  = 10;
  int quantity = 20;

  int *p_pr = &price;
  int *p_qt = (int *)0;//Point to null 
  p_qt = &quantity;
  int total = (*p_pr)*(*p_qt);

  printf("The total price is %d \n",total);
  
  *p_pr = 15;//The price goes up like our city's house price
  quantity = 18;//But the suppling quantities go down
  total = (*p_pr)*(*p_qt);

  printf("The now total price is %d \n",total);

  return 0;
}



From this example ,we assign two new values to the variables one through the pointer variable "p_pr"  and the other through the integar variable "quantity". We may call thses the fundemantal application with pointer. But if we just use a pointer like these,that's so suck! Here, remind me two funny programs about the swap two numbers as below .
static void swap_two(int data1, int data2)
{
  int temp = data1;
  data1 = data2;
  data2 = temp;
}

static void swap_2(int *p_data1, int *p_data2)
{
  int temp = *p_data1;
  *p_data1 = *p_data2;
  *p_data2 = temp;
}


 Both of the examples are listed in some C reference books.Some books said that when our program  call this swap_two() sub routine,we just transmit the copy_ones to the argument but not themselves. so we can't swap the two values. Well, the swap2() what we operate the two value's address. We assign the other value to the address directly. This saying is right, but i don't think that's the key issues.On the contrary make me feel sick.  If you want to know why, we must make clear the scope of a program. When we call a function, how dose this function work?. For example,
static int scope_one(void)
{
  int var1 = 0;
  return 0;
}


Do you know the the scope of the  integer variable "var1" . Surely, this local variable are created in the stack at calling this subroutine. This variable's scope ends when  the scope_one() function returns.This is just the one issue, there are also related issues like if a function call this one, where the function return, if there are several arguments in a function ,what to do?I will fulfill this chapter next time or next next time with some schematic . Using schematic make us understand these conception more easily.
Ok, let go on this article, previous chapter has been introduce the memory allocation, the scope is really depends on the memory. A scope is a region of the program and most time it related with the variables include global variables and local variables (or function parameters). Inner the scope, we can accessed the variables, outer we can't. For example ,
/*Test the scope*/
#include <stdio.h>

int global_var1 = 0;
float global_var2 = 0.0;

static void func1(int pa_var1,int pa_var2)
{
  int local_var1 = 0, local_var2 = 0;

  local_var1 = pa_var1 - pa_var2;
  local_var2 = pa_var1 + pa_var2;

  printf("pa_var1 = %d address : %p\n",pa_var1,&pa_var1);
  printf("pa_var2 = %d address : %p\n",pa_var2,&pa_var2);
  printf("local_var1 = %d address : %p\n",local_var1,&local_var1);
  printf("local_var1 = %d address : %p\n",local_var1,&local_var1);
  
  printf("global_var2 = %f address : %p\n",global_var2,&global_var2);
}

int main(void)
{
  int mlo_var1 = 0;
  printf("mlo_var1 = %d  address: %p\n",mlo_var1,&mlo_var1);
  
  /*This is special one here just two braces*/
  {
    int lo_var2 = 0;
    printf("lo_var2 = %d  address: %p\n",lo_var2,&lo_var2);
  }

  /*Not visible of lo_var2 here, because beyond its scope */
  //printf("lo_var2 = %d  address: %p\n",lo_var2,&lo_var2);

  func1(1,2);
  printf("global_var1 = %d address : %p\n",global_var1,&global_var1);
  return 0;
}


This program is just in one source file, so we can use the global variables anywhere throughout the lifetime of this program, but in multiple sources files, if we define a global variable in a source file, but we used it in another source file, we may declare it as "extern" in referenced source file .Here i draw a schematic to show the scope of a program.



From the schematic ,we can observe the difference type variable divided into difference sections. And the GLOBALS section holds the global variables which scope is visible to the whole program, these global variables are destroyed until the main() function exit. The STACK section load the local variables ,return vectors where is  noted by the thunder lighting flag  and intermediate variables if your subroutines apply some calculation.Here we list the conclusion below table

Variables Scope Declared Section
global the whole program, they can be accessed by any function Usually on top of program GLOBALS
local / parameters Visible in side of a function declared or block of code until return from the function or block of code Inside a function or block of code STACK

Here is an program as below

/*Scope visible or invisible*/
#include <stdio.h>

int var1 = 100;//First, declare var as global variable

static int mul(int a, int b)
{
  return a*b;
}

int main(void)
{
  int var1 = 2;//Second, declare var1 as local variable
  int var2 = 3;
  int var3 = 0;
  var3 = mul(var1,var2);
  printf("The result is %d\n",var3);

  return 0;
}
We have declared "var1" as a global variable at the top of the program, but in main() function we declared the same name variable "var1", too. When we call the mul() and var1 as one of the parameters , what's the result?

Here a method maybe call visible or invisible (block?) , i don't know whether this 'visible' is correct or not. But here i just use this noun visible to express that i can be accessed by a function. In this main() function , the local var1 is visible by the mul() function ,and this local var1 invisible(verb) the global variable. As show in this schematic 


I'm a little tired!  So finish here.  Have a nice weekend,everyone!



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章