Tech Support > Computers & Technology > Programming > query
query
Posted by deshpandeajit@yahoo.com on September 22nd, 2005


Hi,
I have following piece of C-code below. Pls go thru and try to answer
the questions I have below:

void func(char **);
main()
{
char *a[]={"god","father"};//ARRAY OF strings,i.e.

func(a);

}

void func(char **ptr)
{
Now here in function "func" i want to access(printf), the 2 strings
"god" and "father". How can I do so.
e.g. printf("%s %s ",*ptr,*ptr +/- <some pointer offset from the
first pointer value for the first string "god"> );

}//func ends

1.)My question is how can I access/printf the second string "father" by
using variable ptr. How do I calculate the pointer offsets for this
second string from the first one?

2.)Second question : Is there any way by which I can get sizeof the two
strings in bytes, for the definition below -
char *a[]={"god","father"};

wishes,
AD.

Posted by Thad Smith on September 22nd, 2005


deshpandeajit@yahoo.com wrote:
These kind of questions are about language usage, so should be asked in
comp.lang.c or comp.lang.c.moderated. This forum is for
language-independent programming issues, such as algorithms.


I am assuming you have
#include <stdio.h>

printf("%s %s ",ptr[0], ptr[1]);
Using a subscript of 1 accesses the second pointer to char in the array,
etc.

You can use strlen() to get the number of characters in the string
preceding the terminating null. It will yield 3 and 6, respectively,
for your example strings. You need to include header <string.h>.

Thad


Posted by Joe Wright on September 23rd, 2005


deshpandeajit@yahoo.com wrote:
#include <string.h>

void func(char **ppc) {
char *cp;
int i = 0;
while ((cp = ppc[i++]) != NULL) {
printf("%3d %s\n", (int)strlen(cp), cp);
}
}

int main(void) {
char *a[] = {"God", "Father", "Son", "Holy Ghost", NULL};
func(a);
return 0;
}

My apologies to comp.programming. This more properly belongs in comp.lang.c.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---

Posted by deshpandeajit@yahoo.com on September 23rd, 2005


Thanks Joe and Thad. And sorry for the mis placed posting.

AD.


Similar Posts