double pointer help needed

C

cong10

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

int str_to_argv(char *str, int *argc, char **argv)
{
char *tmpstr;
char *token;
int token_count = 0;
int i;


printf("argv %p\t*argv %p\n", argv, *argv);
//get how many token
tmpstr = strdup(str);
token = strtok(tmpstr, " ");
while(1){
if(token != NULL){
token_count ++;
}
else{
break;
}
token = strtok(NULL, " ");
}

*argc = token_count;

//make sure argument are not more than 20 character, else likely
will core dump
argv = (char **) calloc(1, token_count * 20 * sizeof(char));

tmpstr = strdup(str);
token = strtok(tmpstr, " ");
argv[0] = token;
printf("token = %s\n", argv[0]);
i = 1;
while(i != token_count){
token = strtok(NULL, " ");
argv = token;
printf("token = %s\n", argv);
i++;
}

return 0;
}

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

int c;
char **v;
int i;

str_to_argv("na ru wa", &c, v);
printf("c = %d\n", c);

i = 0;
while(i != c){
printf("argv[%d] = %s\n", i, v);
i++;
}

free(v);
return 0;
}

====================
Problem:
====================
Segmentation fault when printf("argv[%d] = %s\n", i, v);
the v pointed change in str_to_argv function which malloc,
but when return back to main... it seems like not getting the new
address..
anyone can help?
====================
 
B

BruceS

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

int str_to_argv(char *str, int *argc, char **argv)
{
    char *tmpstr;
    char *token;
    int token_count = 0;
    int i;

    printf("argv %p\t*argv %p\n", argv, *argv);
    //get how many token
    tmpstr = strdup(str);
    token = strtok(tmpstr, " ");
    while(1){
        if(token != NULL){
            token_count ++;
        }
        else{
            break;
        }
        token = strtok(NULL, " ");
    }

    *argc = token_count;

    //make sure argument are not more than 20 character, else likely
will core dump
    argv = (char **) calloc(1, token_count * 20 * sizeof(char));

    tmpstr = strdup(str);
    token = strtok(tmpstr, " ");
    argv[0] = token;
    printf("token = %s\n", argv[0]);
    i = 1;
    while(i != token_count){
        token = strtok(NULL, " ");
        argv = token;
        printf("token = %s\n", argv);
        i++;
    }

    return 0;

}

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

    int c;
    char **v;
    int i;

    str_to_argv("na ru wa", &c, v);
    printf("c = %d\n", c);

    i = 0;
    while(i != c){
        printf("argv[%d] = %s\n", i, v);
        i++;
    }

    free(v);
    return 0;

}

====================
Problem:
====================
Segmentation fault when printf("argv[%d] = %s\n", i, v);
the v pointed change in str_to_argv function which malloc,
but when return back to main... it seems like not getting the new
address..
anyone can help?
====================


On first glance, I see that you're passing v into str_to_argv(), then
attempting to use it. All arguments in C are passed by value, not
reference, so the value of v in main() isn't changed by
str_to_argv(). It looks like you want to pass &v, and change the
usage in str_to_argv() accordingly.
 
K

kathir

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int str_to_argv(char *str, int *argc, char **argv)
{
char *tmpstr;
char *token;
int token_count = 0;
int i;
printf("argv %p\t*argv %p\n", argv, *argv);
//get how many token
tmpstr = strdup(str);
token = strtok(tmpstr, " ");
while(1){
if(token != NULL){
token_count ++;
}
else{
break;
}
token = strtok(NULL, " ");
}
*argc = token_count;
//make sure argument are not more than 20 character, else likely
will core dump
argv = (char **) calloc(1, token_count * 20 * sizeof(char));
tmpstr = strdup(str);
token = strtok(tmpstr, " ");
argv[0] = token;
printf("token = %s\n", argv[0]);
i = 1;
while(i != token_count){
token = strtok(NULL, " ");
argv = token;
printf("token = %s\n", argv);
i++;
}

return 0;

int main(int argc, char *argv[])
{
int c;
char **v;
int i;
str_to_argv("na ru wa", &c, v);
printf("c = %d\n", c);
i = 0;
while(i != c){
printf("argv[%d] = %s\n", i, v);
i++;
}

free(v);
return 0;

====================
Problem:
====================
Segmentation fault when printf("argv[%d] = %s\n", i, v);
the v pointed change in str_to_argv function which malloc,
but when return back to main... it seems like not getting the new
address..
anyone can help?
====================


On first glance, I see that you're passing v into str_to_argv(), then
attempting to use it. All arguments in C are passed by value, not
reference, so the value of v in main() isn't changed by
str_to_argv(). It looks like you want to pass &v, and change the
usage in str_to_argv() accordingly.



You have to pass the address of the double pointer in the main
function. The following code will work for you. I have changed
char*** in the function str_to_argv.


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

int str_to_argv(char *str, int *argc, char ***argvp)
{
char **argv = NULL;

char *tmpstr;
char *token;
int token_count = 0;
int i;

tmpstr = strdup(str);
token = strtok(tmpstr, " ");
while(1){
if(token != NULL){
token_count ++;
}
else{
break;
}
token = strtok(NULL, " ");
}

*argc = token_count;

//make sure argument are not more than 20 character, else
likelywill core dump

argv = (char **) calloc(1, token_count * 20 * sizeof(char));

tmpstr = strdup(str);
token = strtok(tmpstr, " ");
argv[0] = token;
printf("token = %s\n", argv[0]);
i = 1;
while(i != token_count){
token = strtok(NULL, " ");
argv = token;
printf("token = %s\n", argv);
i++;
}

*argvp = argv;

return 0;

}

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

int c;
char **v = NULL;
int i;

str_to_argv("na ru wa", &c, &v);
printf("c = %d\n", c);

i = 0;
while(i != c){
printf("argv[%d] = %s\n", i, v);
i++;
}

free(v);
return 0;
}

- Kathir

Web: http://www.kathircpp.com
 
C

cong10

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int str_to_argv(char *str, int *argc, char **argv)
{
    char *tmpstr;
    char *token;
    int token_count = 0;
    int i;
    printf("argv %p\t*argv %p\n", argv, *argv);
    //get how many token
    tmpstr = strdup(str);
    token = strtok(tmpstr, " ");
    while(1){
        if(token != NULL){
            token_count ++;
        }
        else{
            break;
        }
        token = strtok(NULL, " ");
    }
    *argc = token_count;
    //make sure argument are not more than 20 character, else likely
will core dump
    argv = (char **) calloc(1, token_count * 20 * sizeof(char));
    tmpstr = strdup(str);
    token = strtok(tmpstr, " ");
    argv[0] = token;
    printf("token = %s\n", argv[0]);
    i = 1;
    while(i != token_count){
        token = strtok(NULL, " ");
        argv = token;
        printf("token = %s\n", argv);
        i++;
    }
    return 0;
}
int main(int argc, char *argv[])
{
    int c;
    char **v;
    int i;
    str_to_argv("na ru wa", &c, v);
    printf("c = %d\n", c);
    i = 0;
    while(i != c){
        printf("argv[%d] = %s\n", i, v);
        i++;
    }
    free(v);
    return 0;
}
====================
Problem:
====================
Segmentation fault when printf("argv[%d] = %s\n", i, v);
the v pointed change in str_to_argv function which malloc,
but when return back to main... it seems like not getting the new
address..
anyone can help?
====================

On first glance, I see that you're passing v into str_to_argv(), then
attempting to use it.  All arguments in C are passed by value, not
reference, so the value of v in main() isn't changed by
str_to_argv().  It looks like you want to pass &v, and change the
usage in str_to_argv() accordingly.

You have to pass the address of the double pointer in the main
function. The following code will work for you.  I have changed
char*** in the function str_to_argv.

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

int str_to_argv(char *str, int *argc, char ***argvp)
{
    char **argv = NULL;

    char *tmpstr;
    char *token;
    int token_count = 0;
    int i;

    tmpstr = strdup(str);
    token = strtok(tmpstr, " ");
    while(1){
        if(token != NULL){
            token_count ++;
        }
        else{
            break;
        }
        token = strtok(NULL, " ");
    }

    *argc = token_count;

    //make sure argument are not more than 20 character, else
likelywill core dump

    argv = (char **) calloc(1, token_count * 20 * sizeof(char));

    tmpstr = strdup(str);
    token = strtok(tmpstr, " ");
    argv[0] = token;
    printf("token = %s\n", argv[0]);
    i = 1;
    while(i != token_count){
        token = strtok(NULL, " ");
        argv = token;
        printf("token = %s\n", argv);
        i++;
    }

    *argvp = argv;

    return 0;

}

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

    int c;
    char **v = NULL;
    int i;

    str_to_argv("na ru wa", &c, &v);
    printf("c = %d\n", c);

    i = 0;
    while(i != c){
        printf("argv[%d] = %s\n", i, v);
        i++;
    }

    free(v);
    return 0;

}

- Kathir

Web:http://www.kathircpp.com


Wow, this is great, and now i understand pointer better.
Thanks to both Bruce S and Kathir. :)
 
J

James Waldby

.
int str_to_argv(char *str, int *argc, char **argv) { ....
argv = (char **) calloc(1, token_count * 20 * sizeof(char));
tmpstr = strdup(str);
token = strtok(tmpstr, " ");
argv[0] = token; ....
while(i != token_count){
token = strtok(NULL, " ");
argv = token;

....

Other posters have given you some useful advice but (I think)
didn't comment re that calloc thing.

1. calloc(1, n) is equivalent to malloc(n) and that is the
form you should use instead, or possibly [see item 4]
calloc(token_count, sizeof(char*)).

2. No need to cast the calloc result that way; see
<http://www.c-faq.com/malloc/cast.html> and the question
after it.

3. Don't use the result of an alloc without testing non-null;
eg, see the "Proper way to check malloc return" thread.

4. argv should end up being an array of pointers to strings.
20 * sizeof(char) is not the number of bytes that it takes to
store a pointer to a string. Instead of what you have, say

argv = malloc (token_count * sizeof(char*));
if (!argv) ...

where ... is error-handling code, or use some of the
variations of error-testing per the thread mentioned in 3.
 
B

Barry Schwarz

int str_to_argv(char *str, int *argc, char **argv) { ...
argv = (char **) calloc(1, token_count * 20 * sizeof(char));
tmpstr = strdup(str);
token = strtok(tmpstr, " ");
argv[0] = token; ...
while(i != token_count){
token = strtok(NULL, " ");
argv = token;

...

Other posters have given you some useful advice but (I think)
didn't comment re that calloc thing.

1. calloc(1, n) is equivalent to malloc(n) and that is the
form you should use instead, or possibly [see item 4]
calloc(token_count, sizeof(char*)).


Not if you expect the allocated memory to be initialized.
 
J

James Waldby

int str_to_argv(char *str, int *argc, char **argv) { ...
argv = (char **) calloc(1, token_count * 20 * sizeof(char));
tmpstr = strdup(str);
token = strtok(tmpstr, " ");
argv[0] = token; ...
while(i != token_count){
token = strtok(NULL, " ");
argv = token;

...

Other posters have given you some useful advice but (I think) didn't
comment re that calloc thing.

1. calloc(1, n) is equivalent to malloc(n) and that is the
form you should use instead, or possibly [see item 4]
calloc(token_count, sizeof(char*)).


Not if you expect the allocated memory to be initialized.


Right, I'd forgotten that calloc zeroes memory and malloc doesn't.
Thanks!
 
K

Keith Thompson

James Waldby said:
On Tue, 25 May 2010 08:18:14 -0700, cong10 wrote: ...
int str_to_argv(char *str, int *argc, char **argv) {
...
argv = (char **) calloc(1, token_count * 20 * sizeof(char));
tmpstr = strdup(str);
token = strtok(tmpstr, " ");
argv[0] = token;
...
while(i != token_count){
token = strtok(NULL, " ");
argv = token;
...

Other posters have given you some useful advice but (I think) didn't
comment re that calloc thing.

1. calloc(1, n) is equivalent to malloc(n) and that is the
form you should use instead, or possibly [see item 4]
calloc(token_count, sizeof(char*)).


Not if you expect the allocated memory to be initialized.


Right, I'd forgotten that calloc zeroes memory and malloc doesn't.
Thanks!


Yes, but remember that it sets it to all-bits-zero, which isn't
necessarily a meaningful pointer value.

I haven't studied the code in depth, but I don't think you need
to initialize the allocated memory anyway; the code doesn't (or
shouldn't) refer to anything in that chunk of memory that it hasn't
assigned a value to.

[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top