The C language is sorted by pointers to array elements

Source: Internet
Author: User

//

Main.c

Pointer_array

//

Created by Ma C on 15/8/2.

Copyright (c) 2015 BJSXT. All rights reserved.

Requirement: Use pointer pointer to output string. You first use a pointer array to create an array of strings, and then define a pointer pointer to the string array and use the string in its output array. At the same time, the contents of the array are sorted in ascending order.

#include <stdio.h>

#include <string.h>

void Ascsort (char **arr,int len)//Receive pointer type array, and length

{

for (int i=0;i<len-1;i++)

{

for (int j=0;j<len-1-i;j++)

{

if (strcmp (arr[j],arr[j+1]) >0)//Ascending sort

{

Char *temp = arr[j];

ARR[J] = arr[j+1];

ARR[J+1] = temp;

}

}

}

}

void PrintArray (char* arr[],int Len,char **p)

{

for (int i=0;i<4;i++)

{

printf ("%s", * (P+i));

}

printf ("\ n");

}

int main (int argc, const char * argv[])

{

char* str[]={"Jony", "Tom", "Smith", "Boy"}; //array of pointer types

Char **p;//defines pointer variables that point to pointers

p = str;

printf ("Before sorting:");

PrintArray (STR,4,P);

Ascsort (str,4);

printf ("After sorting:");

PrintArray (STR,4,P);

return 0;

}

The C language is sorted by pointers to array elements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.