Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

[PROGRAMMING IN C]

[POINTERS AND STRINGS]

APRIL 3, 2019
MODULE 3
ELECTRONICS S4
What are Pointers?
A pointer is a variable whose value is the address of another variable, i.e., direct address of the
memory location. Like any variable or constant, you must declare a pointer before using it to
store any variable address. The general form of a pointer variable declaration is −.

Declaration of Pointer variable

General syntax of pointer declaration is,

datatype *pointer_name;

Data type of a pointer must be same as the data type of the variable to which the pointer variable
is pointing. void type pointer works with all data types, but is not often used.

Here are a few examples:

int *ip // pointer to integer variable float


*fp; // pointer to float variable double
*dp; // pointer to double variable char *cp;
// pointer to char variable

Initialization of Pointer variable

Pointer Initialization is the process of assigning address of a variable to a pointer variable.


Pointer variable can only contain address of a variable of the same data type. In C language
address operator & is used to determine the address of a variable. The & (immediately preceding
a variable name) returns the address of the variable associated with it.

#include<stdio.h>
void
main()
{
int a = 10;
int *ptr; //pointer declaration
ptr = &a; //pointer initialization }

Pointer variablea always point to variables of same datatype. Let's have an example to showcase
this:

#include<stdio.h>

void main()
{ float a;
int *ptr;
ptr = &a; // ERROR, type mismatch }
If you are not sure about which variable's address to assign to a pointer variable while
declaration, it is recommended to assign a NULL value to your pointer variable. A pointer which
is assigned a NULL value is called a NULL pointer.

#include <stdio.h>

int main() { int


*ptr = NULL;
return 0;
}
Pointer assignment

In ordinary assignment involving pointers, the pointer is an alias for its target. In pointer assignment, the pointer is
associated with a target. If the target is undefined or disassociated, the pointer acquires the same status as the
target. The pointer assignment statement has the following form:

pointer-object => target

pointer-object
Is a variable name or structure component declared with the POINTER attribute.

target
Is a variable or expression. Its type and kind parameters, and rank must be the same as pointer-object. It
cannot be an array section with a vector subscript.

How to declare a string?


Before you can work with strings, you need to declare them first. Since string is an array of characters.
You declare strings in a similar way like you do with arrays.

If you don't know what arrays are, we recommend you to check C arrays.

Here's how you declare a string:

char s[5];

How to initialize strings?


You can initialize strings in a number of ways.

char c[] = "abcd";

char c[50] = "abcd";

char c[] = {'a', 'b', 'c', 'd', '\0'};


char c[5] = {'a', 'b', 'c', 'd', '\0'};

Read String from the user


You can use the scanf() function to read a string.

The scanf() function reads the sequence of characters until it encounters a whitespace
(space, newline, tab etc.).

Example 1: scanf() to read a string


#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0; }

Output

Enter name: Dennis Ritchie


Your name is Dennis.
Example 2: gets() and puts()
#include <stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
gets(name); // read str ing
printf("Name: ");
puts(name); // display
string return 0; }

When you run the program, the


output will be:

Enter name: Tom Hanks


Name: Tom Hanks
Passing Strings to Function
Strings can be passed to a function in a similar way as arrays. Learn more about passing array to
a function.

Example 3: Passing string to a Function


#include <stdio.h>
void displayString(char str[]);

int main()
{
char str[50]; printf("Enter string: "); gets(str);
displayString(str); // Passing string to a function.
return 0;
}
void displayString(char str[])
{
printf("String Output: ");
puts(str); }

Strings and Pointers


Similar like arrays, string names are "decayed" to pointers. Hence, you can use pointer with the
same name as string to manipulate elements of the string. We recommended you to check C
Arrays and Pointers before you check this example:
Example 4: Strings and Pointers
#include <stdio.h>

int main(void) {
char name[] = "Harry Potter";
printf("%c", *name); // Output: H
printf("%c", *(name+1)); // Output: a
printf("%c", *(name+7)); // Output: o

char *namePtr;

namePtr = name;
printf("%c", *namePtr); // Output: H
printf("%c", *(namePtr+1)); // Output: a
printf("%c", *(namePtr+7)); // Output: o }

C – String functions

END

You might also like