I have a problem with this code given below when i compile it without pointers it easily runs i want to run it by using pointer it gives me erro can any body runs the programme with pointers.

when i compile this without pointer it runs
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>

void length(char st1[],int i)
{
i=strlen(st1);
cout<<endl<<"length of string st1 is ="<<i;
}
void main()
{ int i;
char st1[15];//,st2[15],st3[15],st4[15];
cout<<"Enter 1 strings :";
gets( st1);
cout<<"string is :"<<st1;
length(st1,i);
getche();
}

when i use pointer it no runs

void length(char *st1[],int *i)
{
* i=strlen(*st1);
cout<<endl<<"length of string st1 is ="<<*i;
}
void main()
{ int i;
char st1[15];//,st2[15],st3[15],st4[15];
cout<<"Enter 1 strings :";
gets( st1);
cout<<"string is :"<<st1;
length(&st1,&i); //gives only one error in this line
getche();
}

well at the first place i dont undstand why u need pointers for doing this , apart from the string (which u need pointer )

well as far as i have guessed u need to print the length of an array.

void length(char *st1[],int *i)
{
* i=strlen(*st1);
cout<<endl<<"length of string st1 is ="<<*i;
}

1. strings are passed by reference , its default so no need of explicitly writing the * in front of it

2. in strlen function u have used * ..? its wrong

3.while calling the function in main program u have written

length (&str , &i)

string is string (char array) , the variable hold the base address of the array itself.so u need to pass just str thats it.

i have remodified ur code n it workd now

#include<iostream>

using namespace std;



void length(char st1[],int *i1)
{
 *i1=strlen(st1);
cout<<endl<<"length of string st1 is ="<<*i1;
}
int main()
{ int i;
char st1[15];//,st2[15],st3[15],st4[15];
cout<<"Enter 1 strings :";
gets( st1);
cout<<"string is :"<<st1;
length(st1,&i); //gives only one error in this line
return 0;
}

I think when you declared the char* thing[]; it declared an array OF char*'s (I think, please correct me if I'm wrong :) )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.