Question

I have a list of things for review in C programming, could you please give me...

I have a list of things for review in C programming, could you please give me an example and a brief explanation for each question... Thank you very much

  • 5. Create functions that can return pointers or functions that can return structs
  • 6. Analyze code that uses predefined strings such as  strlen, strcat, strncat, and  strtok
  • 7. Create syntax that can perform file input and output
  • 8. Distinguish between array and pointer notation and use both in syntax

Homework Answers

Answer #1

5. Create functions that can return pointers or functions that can return structs

//example for returning struct from pointer

#include <stdio.h>
struct student
{
    char name[50];
    int age;
};

// function prototype
struct student getInformation();

int main()
{
    struct student s;

    s = getInformation();

    printf("\nDisplaying information\n");
    printf("Name: %s", s.name);
    printf("\nRoll: %d", s.age);
    
    return 0;
}
struct student getInformation() 
{
  struct student s1;

  printf("Enter name: ");
  scanf ("%[^\n]%*c", s1.name);

  printf("Enter age: ");
  scanf("%d", &s1.age);
  
  return s1;
}

//example of function returning pointer

#include <stdio.h>
int* findLarger(int*, int*);
void main()
{
int numa=0;
int numb=0;
int *result;
    printf("\n\n Pointer : Show a function returning pointer :\n");
   printf("--------------------------------------------------\n");
printf(" Input the first number : ");
scanf("%d", &numa);
printf(" Input the second number : ");
scanf("%d", &numb);   

result=findLarger(&numa, &numb);
printf(" The number %d is larger. \n\n",*result);
}

int* findLarger(int *n1, int *n2)
{
if(*n1 > *n2)
return n1;
else
return n2;
}

6. Analyze code that uses predefined strings such as  strlen, strcat, strncat, and  strtok

//strlen

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    char str[10] = "Holberton";
    size_t length;

    length = strlen(str);

    printf("The length of the string %s is %lu.\n", str, length);
}
output:
The length of the string Holberton is 9.   

//strcat

#include <stdio.h>
#include <string.h>

int main () {
   char str1[50], str2[50];

   //destination string
   strcpy(str1, "This is my initial string");
 
   //source string
   strcpy(str2, ", add this");

   //concatenating the string str2 to the string str1
   strcat(str1, str2);

   //displaying destination string
   printf("String after concatenation: %s", str1);

   return(0);
}

//strncat

// C,C++ program demonstrate functionality of strncat() 
#include <stdio.h> 
#include <string.h> 

int main() 
{ 
    
// Take any two strings 
char src[50] = "efghijkl"; 
char dest[50]= "abcd"; 

// Appends 5 character from src to dest 
strncat(dest, src, 5); 
    
// Prints the string 
printf("Source string : %s\n", src); 
printf("Destination string : %s", dest); 
    
return 0; 
}

output:

Source string : efghijkl
Destination string : abcdefghi

//strtok

// A C/C++ program for splitting a string
// using strtok()
#include <stdio.h>
#include <string.h>

int main()
{
   char str[] = "How-are-you";

   // Returns first token
   char* token = strtok(str, "-");

   // Keep printing tokens while one of the
   // delimiters present in str[].
   while (token != NULL) {
       printf("%s\n", token);
       token = strtok(NULL, "-");
   }

   return 0;
}

output:

How
are
you

7. Create syntax that can perform file input and output

#include <stdio.h>

main() {

   FILE *fp;
   char buff[255];

   fp = fopen("/tmp/test.txt", "r");
   fscanf(fp, "%s", buff);
   printf("1 : %s\n", buff );

   fgets(buff, 255, (FILE*)fp);
   printf("2: %s\n", buff );
   
   fgets(buff, 255, (FILE*)fp);
   printf("3: %s\n", buff );
   fclose(fp);

}

8. Distinguish between array and pointer notation and use both in syntax

//difference

Array Pointer
An array is a collection of elements of similar data type. the pointer is a variable that stores the address of another variable.
An array size decides the number of variables it can store. a pointer variable can store the address of only one variable in it.
Arrays can be initialized at the definition. pointers cannot be initialized at the definition.
Arrays are allocated at compile time. pointers are allocated at runtime.

//code example array

// Program to take 5 values from the user and store them in an array
// Print the elements stored in the array
#include <stdio.h>

int main() {
  int values[5];

  printf("Enter 5 integers: ");

  // taking input and storing it in an array
  for(int i = 0; i < 5; ++i) {
     scanf("%d", &values[i]);
  }

  printf("Displaying integers: ");

  // printing elements of an array
  for(int i = 0; i < 5; ++i) {
     printf("%d\n", values[i]);
  }
  return 0;
}

output:

Enter 5 integers: 1
-3
34
0
3
Displaying integers: 1
-3
34
0
3

//code example pointer

#include <stdio.h>
int main()
{
  int var = 5;
  printf("var: %d\n", var);

  // Notice the use of & before var
  printf("address of var: %p", &var);  
  return 0;
}
var: 5 
address of var: 2686778
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...
You can complete this assignment individually or as a group of two people. In this assignment...
You can complete this assignment individually or as a group of two people. In this assignment you will create a ​​Sorted Singly-Linked List​ that performs basic list operations using C++. This linked list should not allow duplicate elements. Elements of the list should be of type ‘ItemType’. ‘ItemType’ class should have a private integer variable with the name ‘value’. Elements in the linked list should be sorted in the ascending order according to this ‘value’ variable. You should create a...
I did already posted this question before, I did get the answer but i am not...
I did already posted this question before, I did get the answer but i am not satisfied with the answer i did the code as a solution not the description as my solution, so i am reposting this question again. Please send me the code as my solution not the description In this project, build a simple Unix shell. The shell is the heart of the command-line interface, and thus is central to the Unix/C programming environment. Mastering use of...