开发者

I have two questions about c language

1)can we define a void pointer?

such as void * pointer;

is it allowed in C? what is a void pointer?

2) I always hear 开发者_开发技巧about Global Variable, but I do not know how to define Global Variable, I mean where to define a Global Variable? in the beginning of a file or what?


  1. Yes. A void pointer is a pointer to memory where we have not specified what type is at that memory location.

  2. Define them outside of any function or class.


A void pointer is a pointer to any type. A void pointer can be cast to any type and a pointer to any type can be cast to a void pointer.

A global variable is a variable available to all functions in your program (whereas a local variable is available only within the function it's created). A global variable can be declared outside of any function, though typically they're declared right before the main() function. Groups of global variables can be defined at once.


i) Void pointers are allowed in C, they essentially represent a raw address (i.e. no type definition).

ii) A global variable is one that is defined outside of the scope of a function e.g. at the top of a c module.


A C global variable is just a variable defined outside of any function. Ex:

int xyz; /* Global */
int main() {
    return 1;
}


As for the global variables there is no "global" variable in C, although you can define a variable inside a module outside any of the methods and then reference it in other modules by calling external.

A void pointer just points to some data which has an unknown type.


1) of course you can. void pointer are raw pointer. they are used to reference address of memory where are stored data of an unknown type.

2) every variable declared outside function is global. To share it with other modules use keyword extern. (edit: of course in C a variable must be declared before it is used. So a "really" global variable should be declared at the beginning of your module, in order to be able to use it in every function)


You can have void pointers, but you must cast them to pointer of appropriate type before you can dereference them.

int someFunction(type_t type, void *ptr)
{
   type_one_t *t1_ptr = NULL;
   type_two_t *t2_ptr = NULL;
   common_t *c_ptr = NULL;

   if(type == TYPE_1){
     t1_ptr = (type_one_t*)ptr;
     c_ptr = t1_ptr->pcommon;
   }
   else if(type == TYPE_2){
     t2_ptr = (type_two_t*)ptr;
     c_ptr = t2_ptr->pcommon;
   }
   else{
     return INVALID_VAL;
   }

   return c_ptr->someValue;
}

Global variables are declared outside the function.

int a;

void func_1(int b)
{
  int k = someVal();
  a = k + b;
}

void func_2(int c)
{
  a = a + c;
}

here, a is global.


  1. Yes, definitely you can define a void pointer in C language. Example: void * pointer = &variable; Void pointer points to a variable of any type. So when you'll deference the value stored in pointer, you have to use type casting. Thus you can use the pointer to point any data. Such as,
void * pointer = NULL; //declaring

//pointing to an integer
int a = 10;
pointer = &a;
printf("a = %d\n", *(int *)pointer);

//pointing to a double value
double b = 30.56;
pointer = &b;
printf("b = %0.2lf\n", *(double *)pointer);

//pointing to a character value
char c = 'a';
pointer = &c;
printf("c = %c\n", *(char *)pointer);

Thus void pointer is helpful. It is more helpful to use in a function.

  1. Global variable is used when you need a particular variable to use in some of the functions in the program. If you define the variable as global, then you need not to declare the same variable in every function. Though it is not a good practice to declare a global variable. You can pass the value of the variable as pass by reference.
#include <stdio.h> //header file declaration section
#define MAX 50 //section for preprocessors
int a = 20; //global variable declaration section

int main()
{
...................
return 0;
}


Global variables:A global variable is a variable and it can me accessed and modified(if required) throughout your program.

Void pointers:These pointers can be cast to any type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜