0% found this document useful (0 votes)
2 views7 pages

Pointers

The document provides a comprehensive overview of C programming concepts including pointers, dynamic memory allocation, dangling pointers, command line arguments, file modes, file content copying, and various file functions. It includes example code snippets to illustrate each concept. Key functions like malloc(), calloc(), realloc(), and file handling functions such as fprintf() and fscanf() are explained in detail.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Pointers

The document provides a comprehensive overview of C programming concepts including pointers, dynamic memory allocation, dangling pointers, command line arguments, file modes, file content copying, and various file functions. It includes example code snippets to illustrate each concept. Key functions like malloc(), calloc(), realloc(), and file handling functions such as fprintf() and fscanf() are explained in detail.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C Programming Questions and Answers

1. What is Pointer? Explain Dereferencing and Address Operators with Example.

A pointer in C is a variable that stores the address of another variable. It allows you to manipulate

memory directly.

- Dereferencing Operator (*) is used to access the value at the address stored by the pointer.

- Address Operator (&) gives the address of a variable.

Example:

#include <stdio.h>

int main() {

int a = 10;

int *ptr = &a; // pointer to a

printf("Address of a: %p\n", ptr);

printf("Value of a: %d\n", *ptr);

return 0;

2. What is Dynamic Memory Allocation? Explain malloc(), calloc(), realloc(), free().

Dynamic memory allocation allows allocating memory during runtime. Functions used for dynamic

memory allocation are:

- malloc() Allocates a block of memory of a given size.

- calloc() Allocates memory for an array of elements and initializes it to 0.

- realloc() Resizes a previously allocated memory block.

- free() Deallocates memory previously allocated by malloc(), calloc(), or realloc().

Example:
#include <stdio.h>

#include <stdlib.h>

int main() {

int *ptr;

ptr = (int*) malloc(5 * sizeof(int)); // malloc

if (ptr == NULL) {

printf("Memory allocation failed!\n");

return 1;

ptr = (int*) realloc(ptr, 10 * sizeof(int)); // realloc

free(ptr); // free the memory

return 0;

3. What is Dangling Pointer? Example.

A dangling pointer is a pointer that does not point to a valid memory location, often after the memory

it points to has been freed.

Example:

#include <stdio.h>

#include <stdlib.h>

int main() {

int *ptr = (int*) malloc(sizeof(int));


*ptr = 5;

free(ptr); // Memory freed

// ptr is now a dangling pointer

// Dereferencing a dangling pointer can cause undefined behavior

// printf("%d", *ptr); // Dangerous

return 0;

4. Command Line Arguments with Example.

Command-line arguments allow you to pass information to a program at runtime.

Example:

#include <stdio.h>

int main(int argc, char *argv[]) {

if (argc > 1) {

printf("Number of arguments: %d\n", argc);

printf("First argument: %s\n", argv[1]);

} else {

printf("No arguments passed.\n");

return 0;

5. Explain Various Modes of File.

File modes in C:
- 'r' : Read from file.

- 'w' : Write to file (creates file if not exists, truncates if exists).

- 'a' : Append to file.

- 'r+' : Read and write to file.

- 'w+' : Read and write to file (creates file if not exists, truncates if exists).

- 'a+' : Read and append to file.

6. Explain Copying Content of One File to Another.

To copy the contents of one file to another, open both files and read the contents from the source

file and write to the destination file.

Example:

#include <stdio.h>

int main() {

FILE *src, *dest;

char ch;

src = fopen("[Link]", "r");

if (src == NULL) {

printf("Error opening source file.\n");

return 1;

dest = fopen("[Link]", "w");

if (dest == NULL) {

printf("Error opening destination file.\n");


fclose(src);

return 1;

while ((ch = fgetc(src)) != EOF) {

fputc(ch, dest);

fclose(src);

fclose(dest);

return 0;

7. Explain the Functions of Files: 1) fprintf 2) fscanf 3) fputc 4) fgetc 5) fseek 6) fgets 7) fputs 8)

fread 9) fwrite.

1) fprintf() - Writes formatted data to a file.

2) fscanf() - Reads formatted data from a file.

3) fputc() - Writes a single character to a file.

4) fgetc() - Reads a single character from a file.

5) fseek() - Moves the file pointer to a specific location in the file.

6) fgets() - Reads a string from a file.

7) fputs() - Writes a string to a file.

8) fread() - Reads binary data from a file.

9) fwrite() - Writes binary data to a file.

Example Code:

#include <stdio.h>
int main() {

FILE *file;

char str[] = "Hello, World!";

char buffer[50];

file = fopen("[Link]", "w");

fprintf(file, "%s\n", str); // Write formatted data

fclose(file);

file = fopen("[Link]", "r");

fscanf(file, "%s", buffer); // Read formatted data

printf("Read from file: %s\n", buffer);

fputc('A', file); // Write a character

fclose(file);

file = fopen("[Link]", "r");

char ch = fgetc(file); // Read a character

printf("Character read: %c\n", ch);

fseek(file, 0, SEEK_SET); // Set file pointer to beginning

fgets(buffer, 50, file); // Read a string

printf("Read from file: %s\n", buffer);

fputs("Appended text.\n", file); // Write string to file

fclose(file);
file = fopen("[Link]", "rb");

fread(buffer, sizeof(char), 50, file); // Read binary data

fwrite(buffer, sizeof(char), 50, file); // Write binary data

fclose(file);

return 0;

You might also like