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
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT