Want to pass a single char pointer from a double pointer
Asked Answered
M

2

0

I have to write a function which takes in 2 double pointers (both to char type). The first double pointer has a string of query values and the 2nd one has stopwords. The idea is to eliminate the stopwords from the query string and return all the words without those stopwords.

For example

Input - query: “the”, “new”, “store”, “in”, “SF”

    stopwords: “the”, “in”

OUTPUT new store SF

I have written the following code while trying to use strtok which takes in only single pointers to char types. How do I access the contents of a double pointer?

Thanks

#include <stdio.h>

void remove_stopwords(char **query, int query_length, char **stopwords, int stopwords_length) {
    char *final_str;

    final_str = strtok(query[0], stopwords[0]);
    while(final_str != NULL)
    {
        printf("%s\n", final_str);
        final_str = strtok(NULL, stopwords);
    }

}
Morie answered 6/7, 2013 at 4:32 Comment(5)
you could also do it like this : final_str=strtok(*query,*stopwords); and final_str=strtok(NULL,*stopwords); in case of strtok as rest has been described in the answersParliamentarianism
@PHIfounder: Your suggested strtok call would split the first string in the query array using the letters in the first string of the stopwords array as delimiters, which I do not think is the OP's intention.Camire
@Camire That's why I wrote it as comment.Parliamentarianism
A pointer to a pointer is not a "double pointer" ... that's incorrect and confusing terminology.Plebiscite
@JimBalter yes, at first I thought it a pointer to double.Parliamentarianism
C
2

For simplicity's sake, you can assume a double pointer to be equivalent to a 2d array (it is not!). However, this means that you can use array-convention to access contents of a double pointer.

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

char *query[5] = {"the","new","store","in","SF"};
char *stopwords[2] = {"the","in"};
char main_array[256];

void remove_stopwords(char **query,int query_length, char **stopwords, int stopwords_length);

int main()
{
    remove_stopwords(query,5,stopwords,2);
    puts(main_array);
    return 0;
}

void remove_stopwords(char **query,int query_length, char **stopwords, int stopwords_length)
{
    int i,j,found;
    for(i=0;i<query_length;i++)
    {
        found=0;
        for(j=0;j<stopwords_length;j++)
        {
            if(strcmp(query[i],stopwords[j])==0)
            {
                found=1;
                break;
            }
        }
        if(found==0)
        {
            printf("%s ",query[i]);
            strncat(main_array,query[i],strlen(query[i]));
        }
    }
}

Output: new store SF newstoreSF

Copyreader answered 6/7, 2013 at 4:53 Comment(3)
So if i want to retrieve the contents of the query into a single char pointer, can i access it using the syntax *query[i]?Morie
@user2555541: You can concatenate the results into a single char pointerCopyreader
Thanks for your help. How would I do that? Again, using strcat requires access to only single char ptr. Actually that is what I've been trying to do. Move the contents of the double ptr into a single char* and then use the strtok function which eliminates the delimiters.Morie
C
2

@Binayaka Chakraborty's solution solved the problem but I thought it might be useful to provide an alternative that used pointers only and showed appropriate use of strtok(), the use of which may have been misunderstood in the question.

In particular, the second parameter of strtok() is a pointer to a string that lists all the single-character delimiters to be used. One cannot use strtok() to split a string based on multi-character delimiters, as appears to have been the intention in the question.

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

void remove_stopwords(char *query, char **stopwords) {
    char *final_str = strtok(query, " ");
    while(final_str != NULL) {
        int isStop = 0;
        char **s;
        for (s = stopwords; *s; s++) {
            if (strcmp(final_str,*s) == 0) {
                isStop = 1;
            }
        }
        if (!isStop) printf("%s ", final_str);
        final_str = strtok(NULL, " ");
    }
}

int main() {
    const char *q = "the new store in SF";
    char *query = malloc(strlen(q)+1);
    /* We copy the string before calling remove_stopwords() because
       strtok must be able to modify the string given as its first
       parameter */
    strcpy(query,q);
    char *stopwords[] = {"the", "in", NULL};
    remove_stopwords(query,stopwords);
    return 0;
}

The approach shown here also avoids the need to hard code the sizes of the arrays involved, which therefore reduces potential for bugs.

Camire answered 6/7, 2013 at 5:7 Comment(5)
yeah, after seeing the OP's use of strtok(), I didn't want to use it as well :) +1 for removing hardcoding ^_^Copyreader
When I run your code in gdb it indicates segmentation fault.Parliamentarianism
Your code is flawed, char *query="the new store in SF" declares query as a pointer and assigned to constant string meaning it can't be modified while strtok is modifying it, it gives segmentation fault while running with a debugger(GDB). Check for yourself.Parliamentarianism
@PHIfounder: Thank you for your correction. I have amended the code to give strtok() a string that it can modify.Camire
@Camire you are welcome, +1 for dynamically allocating the memory :)Parliamentarianism

© 2022 - 2024 — McMap. All rights reserved.