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();
}

Dani AI

Generated

Short diagnosis for : the compile error comes from a type mismatch. A local char st1[15] decays to char * when used as a function argument, but char *st1[] (a parameter) is really char ** — an array of pointers. Passing &st1 gives the type char (*)[15] (pointer to the whole array) which does not convert to char **. That is why the compiler rejects your call. was right to suggest passing st1 (not &st1); was also right that char* thing[] means an array of char* pointers.

A few practical notes:

  • strlen expects a const char* and returns size_t. Prefer returning the length instead of using an int* out-parameter.
  • Avoid gets() (it is unsafe and removed from modern standards); use std::getline with std::string or std::cin.getline for C-style arrays.
  • In C++ use int main() (not void main) and include the correct headers (<string>, <cstring>, <iostream>).

A simple, modern approach (no pointer-to-pointer confusion) is to use std::string and return the length:

#include <iostream>
#include <string>

std::size_t length(const std::string& s) {
    return s.size();
}

int main() {
    std::string s;
    std::getline(std::cin, s);
    std::cout << "length: " << length(s) << '\n';
}

If you really want to keep the char[15] and pass &st1, use a pointer-to-array parameter (rare, but explict):

#include <iostream>
#include <cstring>

void length(char (*arr)[15], int* out) {
    *out = static_cast<int>(std::strlen(*arr));
}

// call: length(&st1, &i);

If you still get errors, post the exact compiler message — it will usually say the invalid conversion (e.g. char (*)[15] to char **) and that pinpoints the mismatch.

Recommended Answers

All 2 Replies

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 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.