0

I want to create a static array of string in a function , then the return value of this function will assign to another array of string .

but it seems I can't do this assignment array2 = function(); but I think use char * for array of string is ok? what's wrong in my code? thanks

here is my code

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

char * function();
int main(){

    char * array2[100] = {};
    array2 = function();

    // print this array of string
    int i;
    for(i=0;i<strlen(array2);i++){
        printf("%s\n",array2[i]);
    }
    system("pause");
}

char * function(){
    static char * array[100] = {};
    array[0] = "100";
    array[1] = "200";
    return array;
}
9
  • 2
    not sure if this is part of the problem, but you defined a prototype funtion before main but in main, you refer to function Commented Sep 29, 2013 at 6:10
  • 1
    As noted by other comments, this code should not even compile. What compiler are you using? Is the compiler printing warnings when you build this code? Do you have compiler warnings enabled at the maximum level? You should check all of these first--then the compiler would practically solve this problem for you. Commented Sep 29, 2013 at 6:17
  • 1
    Change char ** funtion(){ and try Commented Sep 29, 2013 at 6:22
  • 3
    @Tonmoy: it doesn't have to be static char **function() just because the data it returns is static, which seems to be what you're suggesting. You make a function static if you don't want it to be visible outside the source file (translation unit) in which it is defined. That use of static is unrelated to its use in declaring a variable inside a function. There static means that the variable has a lifetime as long as the program, not merely as long as the function call is in effect. Commented Sep 29, 2013 at 6:37
  • 1
    There's no such thing as a 'const function' in C; that is a concept that applies to member functions in C++, but not to C. The keywords const, volatile and restrict (and _Atomic in C11) are 'type qualifiers'; the keywords static, extern, auto, register (and, surprisingly, typedef, plus _Thread_local added in C11) are 'storage class specifiers'. They do different jobs in the syntax. In a declaration const char **function();, you specify that the function takes an indeterminate (not empty!) but fixed (not varargs) argument list and it returns a const char **. Commented Sep 29, 2013 at 7:33

3 Answers 3

2

You should use char** or char* array[] for array of strings.

In your function(), array is the first address of array of char*.the prototype should be char **function().

And I don't think array's name can be changed.Say int a[3], b[3];, a is a constant, So a = b is not allowed.

Be careful about the spelling mistakes like funtion() function().

Sign up to request clarification or add additional context in comments.

Comments

1

I'd do something like this:

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

static char **function(void)                                                 
{                                                                            
    char **array = malloc(2 * sizeof(*array));                               
    array[0] = "100";                                                        
    array[1] = "200";                                                        
    return array;                                                            
}                                                                            

int main(int argc, char *argv[])                                             
{                                                                            
    char **array2;                                                           
    array2 = function();                                                     

    /* print this array of string */                                         
    int i;                                                                   
    for (i = 0; i < 2; i++)                                                  
        printf("%s\n",array2[i]);                                            

    free(array2);                                                            

    return 0;                                                                
}

Comments

0

There are many ways to solve your problem. It seems to be similar to these questions [general arrays][1] [String arrays][2]

Or you can pass the array instead and get it working like this

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

using namespace std;

void function(char *input[100]){
    input[0] = "100";
    input[1] = "200";
}
int main(){

    char * array2[100];
    function(array2);

    // print this array of string (int)strlen(*array2)
    int i;
    for(i=0;i<(int)strlen(*array2) - 1;i++){
        printf("%s\n",array2[i]);
    }
    system("pause");
}

4 Comments

This question is tagged C not C++, so using namespace std; is out of order; that's C++. You should be consistent in using #include <string.h> rather than then the double-quoted form. It isn't clear why you need #include "stdafx.h" since the code only used standard C functions. The loop control on the for loop is broken; you should not be using strlen() there at all.
using namespace and stdafx is because of IDE. I just want to illustrate the point hence this code...I think the person who asked this question will get pointers to study more instead of complete answer. @JonathanLeffler can you please explain your point about strlen? how is it broken and is it exploitable?
You shouldn't let your IDE interfere with your code (but I'm prejudiced against IDEs in general, so treat with appropriate caution). However, writing C++ code in answer to a C question is always going to get you into trouble. The troubles with strlen() in context are that it returns 3 (because there are 3 digits in "100"), but there are only two strings to be printed (the other pointers are null). The length of the string index[0] is unrelated to the number of strings in the array. Even if it were the correct function to use, you'd not use it there.
Thanks @JonathanLeffler, I will keep your advice in mind. I will to do some code writing to understand the exact behaviour of strlen, but thanks for pointing out

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.