11 Replies - 1523 Views - Last Post: 01 July 2014 - 05:58 AM Rate Topic: -----

#1 coloncapitald  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-June 14

array of char* as function argument

Posted 28 June 2014 - 12:35 PM

Not sure if i'm missing something very obvious here, but to me it appears that in C and C++, I can not write a loop to iterate through an array of char* which was passed as argument to a function, since I do not know a way to find the size of the array.

char** sortPoints(char* input1[],int input2,int input3)
{
	//Write code here
}


I had to write the contents of this function as part of a problem in a programming contest. I had no clue on how to read all the strings in input1 using a loop in C++. The syntax of the Java function was this:

public static String[] sortPoints(String[] input1,int input2,int input3)
    {
        //Write code here
    }


So, I used iterators in Java. But is there a way to get the size of the array in C++?

Is This A Good Question/Topic? 0
  • +

Replies To: array of char* as function argument

#2 jimblumberg  Icon User is online

  • member icon

Reputation: 5344
  • View blog
  • Posts: 16,679
  • Joined: 25-December 09

Re: array of char* as function argument

Posted 28 June 2014 - 12:53 PM

There are two possible ways to iterate through your array. Since you are using a C-string you can use the strlen() function to determine the size of the array. Another way would be to pass the size of the array into the function as well. But since this is C++ why not use a std::string instead. The std::string knows it's size and has many other member functions to help process the string.


Jim
Was This Post Helpful? 0
  • +
  • -

#3 Blindman67  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 138
  • View blog
  • Posts: 615
  • Joined: 15-March 14

Re: array of char* as function argument

Posted 28 June 2014 - 12:55 PM

char arrays should be null terminated;

while(*input1 != '/0'){
       input1++;
}

Was This Post Helpful? 0
  • +
  • -

#4 coloncapitald  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-June 14

Re: array of char* as function argument

Posted 28 June 2014 - 01:02 PM

View Postjimblumberg, on 28 June 2014 - 12:53 PM, said:

There are two possible ways to iterate through your array. Since you are using a C-string you can use the strlen() function to determine the size of the array. Another way would be to pass the size of the array into the function as well. But since this is C++ why not use a std::string instead. The std::string knows it's size and has many other member functions to help process the string.


Jim


1. It's an array of (char*). I can not use strlen() to find the size of array.
2. I can not alter the signature of the function.
3. Since I can't change the input parameters, I'll have to deal with char* only. I can put individual char* strings in std::string objects, but even then I will have to iterate through the array.


View PostBlindman67, on 28 June 2014 - 12:55 PM, said:

char arrays should be null terminated;

while(*input1 != '/0'){
       input1++;
}


It is not a char array, but an array of pointers to characters i.e. an array of strings.
Was This Post Helpful? 0
  • +
  • -

#5 Blindman67  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 138
  • View blog
  • Posts: 615
  • Joined: 15-March 14

Re: array of char* as function argument

Posted 28 June 2014 - 01:31 PM

If its an array of pointers then you must have a declaration that will assign the max size of the array, so you have an upper limit. Any code that adds pointers to the array should ether keep track of the number of entries or add a null pointer as the last pointer. Then use the length or check for a null pointer.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon

Reputation: 5344
  • View blog
  • Posts: 16,679
  • Joined: 25-December 09

Re: array of char* as function argument

Posted 28 June 2014 - 04:17 PM

If you want actual answers you will need to post more content. Then we won't need to guess. Show how the function is to be called, that includes how the various parameters are defined and values assigned. This code should also show how you intend to use the return value.



Jim

This post has been edited by jimblumberg: 28 June 2014 - 04:18 PM

Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,997
  • Joined: 09-June 09

Re: array of char* as function argument

Posted 30 June 2014 - 04:47 PM

You cannot determine the size of an array simply based on a pointer.

You have a few options:

1) Pass the size of the array as a parameter
2) Pass a vector of char* (instead of an array)
3) Create your own struct which encapsulates the array of strings with the size
4)...

This post has been edited by jjl: 30 June 2014 - 04:48 PM

Was This Post Helpful? 0
  • +
  • -

#8 click_here  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 300
  • Joined: 25-November 13

Re: array of char* as function argument

Posted 30 June 2014 - 04:57 PM

In C a string is just a char array ending in '\0'
"hello" == {'h','e','l','l','o','\0'}

In C you may use array notation on a pointer -> Without this, you would not be able to use malloc()'d memory as an array.
char** sortPoints(char* input1[],int input2,int input3)
{
    int i=0, j=0;
    int stringSize=0;
    while(input1[i][j] != '\0')
    {
        stringSize++;
        j++;
    }




Note that you can also use strlen if the input is NUL terminated (i.e. a string input)
strlen(input1[i]);


This post has been edited by click_here: 30 June 2014 - 04:59 PM

Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6996
  • View blog
  • Posts: 14,635
  • Joined: 16-October 07

Re: array of char* as function argument

Posted 30 June 2014 - 05:33 PM

Perhaps if you had clearer parameter names than input1, input2, input3...

If, for instance, your parameters were:
char **sortPoints(char **points, int size, int ascending)


Then you'd know what to do.

Alternately, as noted, you can know when to stop if your array of pointers ends in a null:
char *a[] = { "Charlie", "Alpha", "Bravo", NULL };
for(i=0; a[i]!=NULL; i++) {



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#10 coloncapitald  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-June 14

Re: array of char* as function argument

Posted 30 June 2014 - 11:19 PM

View Postbaavgai, on 30 June 2014 - 05:33 PM, said:

Perhaps if you had clearer parameter names than input1, input2, input3...

If, for instance, your parameters were:
char **sortPoints(char **points, int size, int ascending)


The first parameter is an array of points stored as strings as in "3#4", the second and the third parameters are x and y coordinates of a point. The challenge is to sort the points in the first parameter with ascending distances from the point formed by second and third parameters.

All,

Thanks for giving thought to this. At this point, I am pretty convinced that while keeping the signature of the function in tact, I wouldn't be able to iterate through the array. There is a problem with their problem, at least with C and C++.
Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6996
  • View blog
  • Posts: 14,635
  • Joined: 16-October 07

Re: array of char* as function argument

Posted 01 July 2014 - 05:24 AM

Why are you working with any array of strings, then? You'd have to parse the mess every single time!

You'd reasonably want something like:
typedef struct { int x, y; } Point;

Point parsePoint(const char *);
float distance(Point *, Point *);
void sortPoints(Point *, int size, Point *origin);



Even if you still choose to store your list of points as an array of string, you'll still want a parse function.
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is online

  • member icon

Reputation: 5344
  • View blog
  • Posts: 16,679
  • Joined: 25-December 09

Re: array of char* as function argument

Posted 01 July 2014 - 05:58 AM

Please show how you're trying to call this function. Be sure to include actual samples of what each of your variables contain. For your array of strings be sure to show how the array was defined and populated.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1