Sie sind auf Seite 1von 8

Array with 2-D pointers

Creating a two dimensional array


To keep things simple we will create a two dimensional integer array num having 3 rows
and 4 columns.

int num[3][4] = {
 {1, 2,  3, 4},
 {5, 6,  7, 8},
 {9, 10, 11, 12}
};

Create pointer for the two dimensional array


To access this array with a help of a pointer, we need a pointer to a 2-d array of type int.
We will assign the address of the first element of the array num to the pointer ptr using
the address of & operator.

int *ptr = &num[0][0];

Accessing the elements of the two dimensional array via pointer


The two dimensional array num will be saved as a continuous block in the memory. So,
if we increment the value of ptr by 1 we will move to the next block in the allocated
memory.
#include <stdio.h>

int main()

int num[3][4] = {

{1, 2, 3, 4},

{5, 6, 7, 8},

{9, 10, 11, 12}

};

int *ptr = &num[0][0];

for (i = 0; i < 3; i++)


{

for (j = 0; j < 4; j++)

printf("%d ", *(ptr + (i*4) + j));

printf("\n");

return 0;

Array of strings
We can create a two dimensional array and save multiple strings in it. For example, in
the given code we are storing 4 cities name in a string array city.

char emp[4][12] = {
 "Amit",
 "Himanshu",
 "Girdhar",
 "Gopal"
};

We can represent the emp array as 4 arrays of 12 characters each. The problem with
this approach is that we are allocating 4x12 = 48 bytes memory to the city array and we
are only using 33 bytes. We can save those unused memory spaces by using pointers
as shown below.

char *empPtr[4] = {
 "Amit",
 "Himanshu",
 "Girdhar",
 "Gopal"
};
In the above code we are creating an array of character pointer empPtr of size 4 to store
the name of the four employees. We can represent the array of pointers as 4 strings of
some required size and 4 pointers to these 4 strings.
To access and print the values pointed by the array of pointers we take help of loop as
shown in the following example.
1
#include <stdio.h>
2

3
int main()
4
{
5
char *empPtr[4] = {
6
"Amit",
7
"Himanshu",
8
"Girdhar",
9
"Gopal"
10
};
11
int r, c;
12

13
for (r = 0; r < 4; r++)
14
{
15
c = 0;
16
while(*(empPtr[r] + c) != '\0')
17
{
18
printf("%c", *(empPtr[r] + c));
19
c++;
20
}
21
printf("\n");
22
}
23
return 0;
24
}
25

Array of pointers
We can have the array of pointers which is generally very useful in handling strings of
varying length in a single array. Suppose we have to save 5 names in an array (Ram,
Laxman, Sita, Dashavatar, Hanuman), then to save them we need a two-dimensional
array of 5 rows and 10 columns at least, because the 2-D array cannot be defined with
different columns. Clearly some columns are wasted for some names as they are not of
that much size. Whereas with the help of array of pointers we can use as much memory
needed as shown below: -

// Creating Five different Strings in memory.


  char str1[]= "Ram";
  char str2[]= "Sita";
  char str3[]= "Laxman";
  char str4[]= "Dashavatar";
  char str5[]= "Hanuman";
  char *pointer_name[5];    // An array of pointers to hold char addresses
  // Assigning the addresses of strings to five pointers
  pointer_name[0] = str1;    
  pointer_name[1] = str2;
  pointer_name[2] = str3;
  pointer_name[3] = str4;
  pointer_name[4] = str5;

Now see same array without using pointer

char array_name[5][15] = {"Ram", "Sita", "Laxman", "Dashavatar",


"Hanuman"};

#include<stdio.h>

int main()

int i=0,j=0;

// Creating Five different Strings in memory.


char str1[]= "Ram";

char str2[]= "Sita";

char str3[]= "Laxman";

char str4[]= "Dashavatar";

char str5[]= "Hanuman";

char *pointer_name[5]; // An array of pointers to hold char addresses

// Assigning the addresses of strings to five pointers

pointer_name[0] = str1;

pointer_name[1] = str2;

pointer_name[2] = str3;

pointer_name[3] = str4;

pointer_name[4] = str5;

for(i=0; i<5; i++)

printf("%s\n",pointer_name[i]);

char array_name[5][15] = {"Ram", "Sita", "Laxman", "Dashavatar", "Hanuman"};

for(i=0; i<5; i++)

for(j=0; j<15; j++)

printf("%c",array_name[i][j]);

printf("\n");

return 0;
}

To DO:

int main()
{
int arr[2][3]= {{1,2,3},{4,5,6}};
int i;
arr[1]=arr[2];
for (i=0;i<2;i++)
printf("%d",arr[i]);
return 0;
}

Array of Pointers - 2
  
The declaration

int (*p) [5];

means

p is one dimensional array of size 5, of pointers to integers.

p is a pointer to a 5 elements integer array.

The same as int *p[5];

None of these.

Implement atoi and itoa functions

1. itoa() function converts int data type to string data type.


2. atoi() function converts string data type to int data type.

har* itoa(int num, char* buffer, int base) 

he third parameter base specify the conversion base. For example:- if base
is 2, then it will convert the integer into its binary compatible string or if
base is 16, then it will create hexadecimal converted string form of integer
number.
If base is 10 and value is negative, the resulting string is preceded with a
minus sign
void reverse(char str[], int length)
{
    int start = 0;
    int end = length -1;
    while (start < end)
    {
        swap(*(str+start), *(str+end));
        start++;
        end--;
    }
}
  
// Implementation of itoa()
char* itoa(int num, char* str, int base)
{
    int i = 0;
    bool isNegative = false;
  
    /* Handle 0 explicitely, otherwise empty string is printed for 0 */
    if (num == 0)
    {
        str[i++] = '0';
        str[i] = '\0';
        return str;
    }
  
    // In standard itoa(), negative numbers are handled only with 
    // base 10. Otherwise numbers are considered unsigned.
    if (num < 0 && base == 10)
    {
        isNegative = true;
        num = -num;
    }
  
    // Process individual digits
    while (num != 0)
    {
        int rem = num % base;
        str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0';
        num = num/base;
    }
  
    // If number is negative, append '-'
    if (isNegative)
        str[i++] = '-';
  
    str[i] = '\0'; // Append string terminator
  
    // Reverse the string
    reverse(str, i);
  
    return str;
}
  
// Driver program to test implementation of itoa()
int main()
{
    char str[100];
    cout << "Base:10 " << itoa(1567, str, 10) << endl;
    cout << "Base:10 " << itoa(-1567, str, 10) << endl;
    cout << "Base:2 " << itoa(1567, str, 2) << endl;
    cout << "Base:8 " << itoa(1567, str, 8) << endl;
    cout << "Base:16 " << itoa(1567, str, 16) << endl;
    return 0;
}
_

/ A simple C program for implementation of atoi

#include <stdio.h>
  
// A simple atoi() function
int myAtoi(char* str)
{
    int res = 0; // Initialize result
  
    // Iterate through all characters of input string and
    // update result
    for (int i = 0; str[i] != '\0'; ++i)
        res = res * 10 + str[i] - '0';
  
    // return result.
    return res;
}
  
// Driver program to test above function
int main()
{
    char str[] = "89789";
    int val = myAtoi(str);
    printf("%d ", val);
    return 0;
}

Q Given a string, you need to test the characters for their uniqueness. If all characters
are occuring at most 1 time in the string print “YES”, otherwise if some character occurs
at least twice in the string print “NO”.

Q Given two strings, check weather second string occurs at end of first string or not.

Q Given a string of words, reverse the order of words in the string individually, not the
whole string.s

Das könnte Ihnen auch gefallen