Explain declaration of pointer variables, initialization and accessing a variable through its pointer. Explain how union is declared and how union member is accessed. Explain realloc() and free() function with example. Explain array within structures with example. Explain file opening and closing in C.
Question
Explain declaration of pointer variables, initialization and accessing a variable through its pointer.
Explain how union is declared and how union member is accessed.
Explain realloc() and free() function with example.
Explain array within structures with example. Explain file opening and closing in C.
Solution
- Declaration of Pointer Variables, Initialization, and Accessing a Variable Through its Pointer:
In C, a pointer is a variable that stores the memory address of another variable. To declare a pointer variable, we use the asterisk () operator. For example, int *p; declares a pointer to an integer. To initialize a pointer, we assign it the address of a variable. For example, if we have int x = 10;, we can initialize the pointer p with p = &x;. The & operator gets the address of a variable. To access the value of the variable the pointer is pointing to, we use the dereference operator (). For example, *p will give us the value of x.
- Declaration of Union and Accessing Union Member:
A union in C is a special data type that allows storing different types of variables in the same memory location. A union is declared using the union keyword. For example, union Data {int i; float f; char str[20];}; declares a union with an integer, a float, and a string. To access a union member, we use the dot (.) operator. For example, if we have union Data data;, we can access the integer with data.i.
- realloc() and free() Function:
The realloc() function is used to resize a block of memory that was previously allocated. For example, if we have int* ptr = malloc(10 * sizeof(int));, we can resize it with ptr = realloc(ptr, 20 * sizeof(int));. The free() function is used to deallocate memory that was previously allocated. For example, free(ptr); will deallocate the memory pointed to by ptr.
- Array Within Structures:
In C, we can have arrays within structures. For example, struct Student {char name[20]; int marks[5];}; declares a structure with a string and an array of integers. To access an element of the array, we use the dot (.) operator and array indexing. For example, if we have struct Student s;, we can access the first mark with s.marks[0].
- File Opening and Closing in C:
In C, we use the fopen() function to open a file. For example, FILE* file = fopen("file.txt", "r"); opens a file in read mode. We use the fclose() function to close a file. For example, fclose(file); closes the file.
Similar Questions
1) What are Macros? Explain macros with an example program?2) What are structure pointers? .Write a C program that uses structure pointer to refer structure members(attributes) using the arrow (->) operator?3) Write a C program that uses “array of structures” to store 3(three) “BOOK” records based on“Book_Pages, Book_Price and Book_Name” and to display all the records of 3(three) Books.4) Explain an Array of pointers with syntax and examples.5) Explain about malloc(), calloc() and realloc() with examples? Differentiate between malloc()and calloc().6) What do you mean by conditional compilation in preprocessing directives? Explain with examples.7) Explain the concept of pointers to structures and self referential structure with examples.
1) Explain the use of typedef keyword and #define preprocessor directive.2) How to include user defined header files into your program? Give an example.3) What are the situations where a pointer becomes a dangling pointer? Explain with an example.4) Explain the enum with an example.5) Explain about Nested Structures with examples?
11) What is the use of the enum keyword in C?12) What is Structure padding?13) What is the use of the bit field in C?14) What is meant by an anonymous structure?15) What is meant by nested structures?16) What are self referential structures?17) What is the use of #include preprocessor directive?18) What is the use of .(dot) operator?19) What is the use of ->(arrow) operator?20) What is the use of pointer variables in C?
ObjectiveIn this challenge, you will learn to implement the basic functionalities of pointers in C. A pointer in C is a way to share a memory address among different contexts (primarily functions). They are primarily used whenever a function needs to modify the content of a variable that it does not own.In order to access the memory address of a variable, , prepend it with sign. For example, &val returns the memory address of .This memory address is assigned to a pointer and can be shared among various functions. For example, will assign the memory address of to pointer . To access the content of the memory to which the pointer points, prepend it with a *. For example, *p will return the value reflected by and any modification to it will be reflected at the source (). void increment(int *v) { (*v)++; } int main() { int a; scanf("%d", &a); increment(&a); printf("%d", a); return 0; } TaskComplete the function void update(int *a,int *b). It receives two integer pointers, int* a and int* b. Set the value of to their sum, and to their absolute difference. There is no return value, and no return statement is needed.Input FormatThe input will contain two integers, and , separated by a newline.Output FormatModify the two values in place and the code stub main() will print their values.Note: Input/ouput will be automatically handled. You only have to complete the function described in the 'task' section.Sample Input45Sample Output91Explanation
11) List out any 3 differences between malloc and calloc functions in dynamic memory allocation?12) List out any six of the preprocessor directives available in the C programming language?13) Write a C program to pass structure as an argument to the functions using call by value method?14) Write a C program to demonstrate nested structure?15) List out any 3 differences between structure and Unions?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.