1
votes

I have an array of pointers to strings (char ** array), and I have a pointer, which points somewhere into this array (for example, third char of array[0]). I need use strncmp, where as second argument, I would pass string starting from where the pointer is.

i.e.:

char ** array = (char**) malloc (sizeof(char*)*count);

array[0] = "Hello World";
char * p = array[0] + 3; /* just for example */
strncmp(query, ?, somemagicnumber);

what '?' should contain, in this case, 'lo World' (p points to second l in Hello);

How do I do this? Is it even possible?

Thanks for your help.

1
I'm having a hard time understanding the question. Do you want strncmp(query, p, somemagicnumber);?Edward
Your array[0] = "Hello World" expression is wrong, to copy the string into array[0] use strcpy(array[0], "Hello World"). Since you are doing this array[0] = malloc(n). And hence you are overwriting array[0] with another address.Iharob Al Asimi
@iharob The assignment is perfectly fine - he does not need to make a copy (although he certainly can make it if he wants to) Of course he's got a memory leak there.Sergey Kalinichenko
@dasblinkenlight Although the malloc above becomes completely useless and the memory is leaked, suggesting its not what the author intended. But sure per definition you're right, it's certainly valid, but stupid, to do so.Jite
@Jite Yes - he needs to either remove the second malloc, or use strcpy like iharob suggests.Sergey Kalinichenko

1 Answers

2
votes
int strncmp(char *string1, char *string2, int n);

This is the prototype of strncmp()

In your case pass

strncmp("somestring",p,n); /* n = number of characters to be compared */