-2

I am trying to write a program for my class but I can't get started because I don't know how to access the function's argument elements. A char array is passed into the function like this:

RPN_calculator(input1)

where input1 is a pointer to an array and the function starts like this

int RPN_calculator(const char** input)
{
    int n = strlen(*input);
    printf("LENGTH OF INPUT: %d\n", n);
    return 0;
}

I was trying to find the length of the array and then iterate through the array but anything I've tried does not print the right length of the array and I can't seem to figure out how to access any of the elements of 'input' (the print statement was just for debugging)

EDIT: even when I calculate n as:

int n = sizeof(*input)/sizeof(*input[0]);

it doesn't work

  • 3
    Welcome to Stack Overflow. Please read the [About] and [Ask] pages soon. You made a decent stab at creating an MCVE ([MCVE]), but you have omitted the code that shows how the `input1` variable that is passed to the function is defined and populated, and that could be rather important. If you have `char input1[256];`, then the call you show doesn't match the function you show, so you've used something else — and it matters what you've used. – Jonathan Leffler Mar 24 '18 at 20:19
  • Show us how you declare `input1` and other relevant parts of the code. – machine_1 Mar 24 '18 at 20:31
  • There are no arrays in your code. `input` is a pointer and should be treated as such. Read up on that. – DeiDei Mar 24 '18 at 20:40
  • A function which operates on strings typically accepts a `char *`. Using a `char **` in such a case is unusual, and probably unnecessary here. – Steve Summit Mar 24 '18 at 21:16
  • With nothing known about the array, it's not possible to calculate how many elements it has from a pointer to it. One convention often used in cases like this is that the last element of the array is a NULL char pointer. Have you written the code that calls RPN_calculator yourself? – Arndt Jonasson Mar 25 '18 at 04:58

1 Answers1

0

I hope this source code will help you with the issue. It is a simple example program that demonstrates how to access any of the strings character by character and also how to find the size of the strings.

#include <stdio.h> 
#include <stdlib.h>
#include <string.h>

#define NUMBER_OS_CHAR_STRINGS  5 

/* Here input is a pointer to an array of strings */
const char *input[NUMBER_OS_CHAR_STRINGS] = {
    "ONE", /*string0. string[0][0] = 'O' -> first element of string0 - O
                        string[0][1] = 'N' -> second element of string0 - N
                        string[0][2] = 'E' -> third element of string0 - E  */ 

    "TWO", /*string1*/ 
    "THREE", /*string2*/
    "FOUR", /*string3*/
    "FIVE", /*string4*/
};


int RPN_calculator(const char **input);
void itterate (const char **input, int choosen_string); 

int main(void) {

    int string_to_itterate = 0;

    RPN_calculator(input);

    printf("Select the string which you would like to print char by char:\n");
    scanf("%d", &string_to_itterate);

    itterate(input, string_to_itterate);

    return(0);
}

int RPN_calculator(const char** input)
{
    int i;

    for (i = 0; i < NUMBER_OS_CHAR_STRINGS; i++)
    {
        int n = strlen(input[i]);
        printf("LENGTH OF INPUT: %d\n", n);
    }
    return 0;
}

/*Simple example function which itterates throught the elements of a chossen string and prints them 
one by one.*/
void itterate (const char **input, int choosen_string)
{
    int i;
    /*Here we get the size of the string which will be used to define the boundries of the for loop*/
    int n = strlen(input[choosen_string]);

    for (i = 0; i < n; i++)
    {
        printf("The %d character of string[%d] is: %c\n",i+1, choosen_string, input[choosen_string][i] ); /*Here we print each character of the string */
    }
    return;
}
Iliya Iliev
  • 151
  • 4