简体   繁体   中英

How to pass in a array string to c function?

I have a string array. Essentially a 2d array of characters.

//Array to hold ip address of people on local network
char* ipArray[100][17];

How would I declare and write a function that passes in this array so that inside of the function I can add ip address, or strings to the array?

This is what I have now, and I get the error error: array type 'char *[17]' is not assignable

//This function takes a string and returns the ip adress
void addIP(char* str, int index, char* arr[][17]){

//Add the ip address to the index that was passed in.
arr[index] = str;


}

If you want to store the actual characters (as opposed to a pointer) in the array, the array should be declared as char ipArray[100][17] (and similarly for the argument to the function). Then, inside the function, you would need to copy str to arr[index] using strcpy or something similar. You would also need to make sure that the length of the string is less than 17.

As others have mentioned, char* var[n][n2] is not an array of strings. Rather an array with 100 slots, where each slot can contain 17 pointers to a character, not characters themselves(if you wanted to get fancy you could use that as a 3D array with constant pointers or dynamic memory, but don't go there).

If you simply want an array where each slot can point to a string via a[slot] = str it's one thing. But you probably wanted to copy the entire string into it, so you would need to use memcpy (or many other variations like strcpy and such) and correct array declaration char var[n][n2] :

main.cpp

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

//This function takes a string and returns the ip adress
void addIP(char* str, int index, char arr[][17])
{
    //Add the ip address to the index that was passed in.
    memcpy(arr[index], str, strlen(str));
}

int main()
{
    //Array to hold ip address of people on local network
    char ipArray[100][17];

    // Clean array and check starting contents.
    memset(ipArray, 0, sizeof(ipArray));
    puts("-------------------------\nStarting array:\n");
    for (int i = 0; i < sizeof(ipArray) / sizeof(ipArray[0]); i++)
    {
        printf("ipArray[%i] = %s\n", i, ipArray[i]);
    }

    addIP("127.0.0.1", 0, ipArray);

    // Check contents.
    puts("-------------------------\nModified array:\n");
    for (int i = 0; i < sizeof(ipArray) / sizeof(ipArray[0]); i++)
    {
        printf("ipArray[%i] = %s\n", i, ipArray[i]);
    }

    return 0;
}

Make sure you know what you are doing when it comes to correct sizes. If there is risk the strings can be larger than what you declared them in the array as, you are asking for trouble. Also, be cautious about null termination(which defines a C-string).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM