By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,346 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

How to change character string in function call?

P: n/a
I want have one string array and want to assign them in
one sub-function. After that,it will be used in main funcion.
But it seems the following code doesn't work.How can I
do it with pointer?

#include <stdio.h>
#include <stdlib.h>

char *str[] = {"word1","word2"};
void sub(char **p)
{

p = str;

}
main()
{
char** argv= NULL;
sub(&argv);
printf("arg[0]=%s\n",argv);
printf("arg[1]=%s\n",argv+1);
}

=========Result=======
[linuxred@vike tmp]$ a.out
arg[0]=(null)
Segmentation fault

Oct 4 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
#include <stdio.h>
#include <stdlib.h>
I found this version works. But too complex argument.

void sub(char ***p)
{
char str[100][100];
strcpy(str[0],"word1");
strcpy(str[1],"word2");

char ***q;
*p = str[0];
*(p+1) = str[1];

}
main()
{
char* argv[100];
sub(&argv);
printf("arg[0]=%s\n",argv[0]);
printf("arg[1]=%s\n",argv[1]);
}

Wavelet wrote:
I want have one string array and want to assign them in
one sub-function. After that,it will be used in main funcion.
But it seems the following code doesn't work.How can I
do it with pointer?

#include <stdio.h>
#include <stdlib.h>

char *str[] = {"word1","word2"};
void sub(char **p)
{

p = str;

}
main()
{
char** argv= NULL;
sub(&argv);
printf("arg[0]=%s\n",argv);
printf("arg[1]=%s\n",argv+1);
}

=========Result=======
[linuxred@vike tmp]$ a.out
arg[0]=(null)
Segmentation fault
Oct 4 '06 #2

P: n/a
"Wavelet" <wa*********@gmail.comwrote:
I want have one string array and want to assign them in
one sub-function. After that,it will be used in main funcion.
But it seems the following code doesn't work.How can I
do it with pointer?

#include <stdio.h>
#include <stdlib.h>

char *str[] = {"word1","word2"};
void sub(char **p)
{

p = str;

}
<http://c-faq.com/aryptr/pass2dary.html>
<http://c-faq.com/aryptr/ary2dfunc2.html>
<http://c-faq.com/ptrs/passptrinit.html>

Not a complete answer, but that should set you on the right track at
least.

Richard
Oct 4 '06 #3

P: n/a

Wavelet ha scritto:

I found this version works. But too complex argument.
try this:

void sub(char **p)
{
*p = *str;
p++;
*p = *(str+1);
}
in main()

char *argv[2];
sub(argv);
printf("arg[0]=%s\n",*argv);
printf("arg[1]=%s\n",*(argv+1));

Oct 4 '06 #4

P: n/a

"Wavelet" <wa*********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>I want have one string array and want to assign them in
one sub-function. After that,it will be used in main funcion.
But it seems the following code doesn't work.How can I
do it with pointer?

#include <stdio.h>
#include <stdlib.h>

char *str[] = {"word1","word2"};
void sub(char **p)
{

p = str;

}
main()
{
char** argv= NULL;
sub(&argv);
printf("arg[0]=%s\n",argv);
printf("arg[1]=%s\n",argv+1);
}
#include <stdio.h>

char *str[] = {"word1","word2"};
void sub(char ***p)
{
*p = str;
}

int main() {
char** argv;
sub(&argv);
printf("arg[0]=%s\n",argv[0]);
printf("arg[1]=%s\n",argv[1]);
return 0;
}
>
=========Result=======
[linuxred@vike tmp]$ a.out
arg[0]=(null)
Segmentation fault
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Oct 4 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.