2012-02-29 29 views
-3

我目前擁有的形式C指向char **到另一個起始位置?

char** args = { "a", "s", "d", "f" }; 

我要的是

char** newArgs = { "s", "d", "f" }; 

怎樣做最簡單的方法的東西嗎?

謝謝。

+0

無效C.我想你應該意味着'字符* ARGS [] = { 「一」, 「S」, 「d」, 「F」};'。 – 2012-02-29 21:14:22

回答

9

也許這樣的:

newargs = args + 1; 

或許:

newargs = &args[1]; 
+0

非常感謝。我知道這不是最好的問題... – JDS 2012-02-29 22:01:24

1

你可以使用

newArgs = &args[1]; 
+0

請在發佈投票時發佈動機... – Saphrosit 2012-02-29 21:19:17

+0

動機是,你的第一個版本完全錯了。 – Nobody 2012-02-29 21:21:48

+0

@我沒有人打字太快。無論如何,我在一分鐘之內糾正自己。不知道這是否值得3票下來... – Saphrosit 2012-02-29 21:24:18

3

如果你想要一個獨立的組指針:

char **newArgs = calloc(3, sizeof(*newArgs)); 
memcpy(newArgs, args + 1, 3*sizeof(*newArgs)); 

否則:

char **newArgs = args + 1; 
+1

爲什麼要創建一個副本? – ThiefMaster 2012-02-29 21:13:13

+0

@ ThiefMaster:取決於目標是什麼。 – 2012-02-29 21:13:38

相關問題