Cプログラミング
 Computer >> コンピューター >  >> プログラミング >> Cプログラミング

C言語を使用して文字列のポインタを作成するにはどうすればよいですか?


(文字列への)ポインタの配列

ポインタの配列は、要素が文字列のベースアドレスへのポインタである配列です。

次のように宣言および初期化されます-

char *a[3 ] = {"one", "two", "three"};
//Here, a[0] is a ptr to the base add of the string "one"
//a[1] is a ptr to the base add of the string "two"
//a[2] is a ptr to the base add of the string "three"

利点

  • 文字の2次元配列のリンクを解除します。 (文字列の配列)では、文字列へのポインタの配列には、ストレージ用の固定メモリサイズはありません。

  • 文字列は必要な数のバイトを占有するため、スペースを無駄にすることはありません。

例1

#include<stdio.h>
main (){
   char *a[5] = {“one”, “two”, “three”, “four”, “five”};//declaring array of pointers to
   string at compile time
   int i;
   printf ( “The strings are:”)
   for (i=0; i<5; i++)
      printf (“%s”, a[i]); //printing array of strings
   getch ();
}

出力

The strings are: one two three four five

例2

文字列のポインタの配列に関する別の例を考えてみましょう-

#include <stdio.h>
#include <String.h>
int main(){
   //initializing the pointer string array
   char *students[]={"bhanu","ramu","hari","pinky",};
   int i,j,a;
   printf("The names of students are:\n");
   for(i=0 ;i<4 ;i++ )
      printf("%s\n",students[i]);
   return 0;
}

出力

The names of students are:
bhanu
ramu
hari
pinky

  1. C言語の線形検索を使用して配列内の最小要素を見つける方法は?

    Cプログラミング言語は、2種類の検索手法を提供します。それらは次のとおりです- 線形検索 二分探索 線形探索 キー要素の検索は直線的に行われます。 これは最も簡単な検索手法です。 リストが並べ替えられることは想定されていません。 制限-より多くの時間を消費し、システムの電力を削減します。 Input (i/p): Unsorted list of elements, key. Output (o/p): 成功–キーが見つかった場合。 失敗–それ以外の場合。 例1 以下は、線形探索を使用して配列内の最小要素を見つけるCプログラムです- #include<stdio.h&

  2. Javaを使用してJSON配列を作成/作成する方法は?

    Json配列は、角かっこで囲まれた順序付けられた値のコレクションです。つまり、「[」で始まり、「]」で終わります。配列の値は「、」(コンマ)で区切られます。 サンプルJSON配列 {    "books": [ Java, JavaFX, Hbase, Cassandra, WebGL, JOGL] } json-simpleは、JSONオブジェクトを処理するために使用される軽量ライブラリです。これを使用すると、Javaプログラムを使用してJSONドキュメントのコンテンツを読み書きできます。 JSON-単純なMaven依存関係 以下は、JSON-si