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...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...