-1

I have been rethinking my way of asking. so i edited the question.

i know beforehand how many strings there will be in the array because it is menu text. suppose i have an array of strings declared as so:char *menu_item[4]; this should give me an array that can hold 4 strings right?

i want to be able to pass the string array to other functions so is this the way to do it?

This array is not declared in main. but in a function that is called from main.

  1. will i have to allocate memory to use fx strcpy(menu_item[1],"some text"); and if yes. what should i allocate?? or is menu_item[0] = "some text"okay??

  2. i have a function in a function to print out the strings which takes a char *string as parameter. and the function itself takes a string array as so char *items[] it looks like this:

    void scroll_menu(char *items[], int size){
        for(int i = 0; i < size; i++){
            print_out(items[i]);
        }    
    }
    

    is the parameter correct for a string array?

i have been looking through a lot of question online. and cant seem to find anything that solves my problem.

if any doubt i will recap what it is i want:

  1. i want to declare an array of strings which holds a known number of strings. sometimes the strings can be initialized in the declaration of the array. and other times i have to look up somewhere and then "assign" or copy the result to the array.
  2. i want to be able to copy or assign strings to it from functions returning a char *string like so strcpy(menu_item[0], some_function_returning_string());

my problem is that i have tried so many different things that i am left confused. and have misunderstood the string array operations.

i have also tried with char menu_items[4][20]; and then strcpy(menu_items[0], "some text"); without any luck. and then there is the issue with how to make the function accept an array declared like this.

any suggestions on how to accomplish what i want would be very nice.

EDIT

i took some time reading the c programming book and found what i was looking for. if i declare an array of pointers to strings as so char *menu_items[4] i will have an array that can take 4 pointers to strings or char arrays char *string

if i want to assign a string from a function returning a char * i have a function as so:

char *function_returning_string(int x){
    static char *strings[3] = {"this","is","strings"};

    if(x <= 2){
        return strings[0];
    }
    else if(x > 2){
        return string[1];
    }
    else{
        return strings[2];
    }
}

the code from where i call this function i have the following:

static char *other_strings = {"yes", "no"};

char *menu_item[3];

menu_item[0] = "ok";
menu_item[1] = other_strings[0];
menu_item[2] = function_returning_string(3);

then i can just pass the entire string array to functions that takes a *string_array[] as parameter as so function_takin_string_array(menu_item); or any string inside to functions taking char *string as parameter as so function_taking_string(menu_item[2]);

the code compiles without errors and works. if anybody think there is something wrong about the code or if have have misunderstood something please let me know.

  • 1
    You have so many errors here. Try to use the debugger and check what is wrong yourself. – 0___________ May 01 '18 at 15:30
  • `static char *addr; sprintf(addr, "S %d", i + 1); return addr;` Couple of questions: 1) where do you allocate memory for `addr`. 2) I just want to make sure you understand how `static` works in this case. – MFisherKDX May 01 '18 at 15:30
  • I see what you mean. It was because I read somewhere that to return a string in "" quotes. It should be static. Guess I haven't corrected that. But I have been staring blindly for hours. So thanks for pointing out that issue. And also that the pointer isn't allocated. – Morten Brask Jensen May 01 '18 at 16:40

1 Answers1

0

As MFisherKDX noted this part of a code is incorrect:

 static char *addr;
 sprintf(addr, "S %d", i + 1);
 return addr;

How do you think what are you returning here? It should be failed on sprintf() call because you try to assign some value to unallocated memory. It should be like this:

static char addr[128];
sprintf(addr, "S %d", i + 1);
return addr;

And how PeterJ_01 mentioned above try to use gdb to figure out what is wrong and where you have other mistakes.

Nick S
  • 1,299
  • 1
  • 11
  • 23