Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

Function memchr in C programming

September 19, 2013 Posted by: GPA Views: 26828 0 comments

Problem

How does memchr() work? Show a simple program with this function.

 Solution

In this article, you will know how to use memchr() function of header file <string.h>.

Declaration:

void *memchr( const void *str, int c, size_t n );

This function searches for the first occurrence of a byte (c) in the specified number of bytes (n) of the string pointed to by the argument str. If the byte is found, it returns a pointer to the byte in the string; otherwise, it returns a NULL pointer.

Example:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>//for function memchr
//main function
int main(int argc, char *argv[])
{
char *str = "This is a test string"; //input string
printf("globalproganswer.com\n");
printf("Input string (str) is: %s\n",str);
//call function memchr
printf("Input string (str) after memchr(str,'t',14) is : %s",memchr(str,'t',14));
printf("\n\n");//new line
system("PAUSE"); //dealy
return 0;
}

Output

Function memchr

Download this example

0 Comments...

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe