# 14. Longest Common Prefix 筆記
這題題目裡有給定一個雙重指標,簡介一下雙重指標的用法
```c=
char* longestCommonPrefix(char** strs , int strsSize)
```
## 雙重指標
一般的指標變數


雙重指標變數

可以簡單從圖了解到雙重指標的作法(a pointer of a pointer)。下方實作此方法:
```c=
#include <stdio.h>
int main()
{
int a = 5;
int *ptr1 = &a;
int **ptr2 = &ptr1;
printf("The address of a is %p\n", &a);
printf("The address of ptr1 is %p\n", &ptr1);
printf("The address of ptr2 is %p\n", &ptr2);
printf("The value of ptr1 is %p\n", ptr1);
printf("The value of ptr2 is %p\n", ptr2);
printf("The value of *ptr2 is %p\n", *ptr2);
printf("value of a: %d\n", a);
printf("value of *ptr1(point to variable a): %d\n", *ptr1);
printf("value of **ptr2(point to variable a): %d\n", **ptr2);
return 0;
}
```
## 題目
那這題的strs部分使用到雙重指標是為了實現二維的字串,有關於這部分之後在整理相關程式與講解,這題作法就是去讀取二維的字串進行比較,透過兩個迴圈去實現。
```c=
char* longestCommonPrefix(char** strs , int strsSize) {
char* temp;
if(strsSize <= 0)
return "";
temp = strs[0];
for(int i=1; i<strsSize; i++){
int j=0;
while(temp[j] && strs[i][j] && temp[j]==strs[i][j])
j ++;
temp[j] = '\0';
}
return temp;
}
```
###### tags: `LeetCode`