logo
down
shadow

How to flip a Char array with pointers in C++


How to flip a Char array with pointers in C++

By : Kenny.Woo
Date : November 21 2020, 11:01 PM
wish help you to fix your issue the function of my program is to simply have a char array with a first and last name, and switch it so it is last name first name. (Tom,Cruise --> Cruise,Tom). I have just started using pointers and cannot find a way to rearrange the Char array. I have tried for-loops and while loops but I don't really know how to tackle this problem. I don't necessarily need hard code. Even a suggestion would help get me moving in the right direction. I am not getting any errors. Due to requirements I am unable to modify the main function whatsoever and the function header has to be , You can use this function:
code :
void LastFirstName(char* first_last_name)
{
    char first_name[41] = {0};
    char last_name[41]  = {0};

    // Dunno what is your separator ' ' or ','...
    char* sep_index = strchr(first_last_name, ','); // This return the first index of ','.
    *sep_index = '\0';

    strcpy(first_name, first_last_name); // Lets copy first name.
    strcpy(last_name,  first_last_name + strlen(first_last_name) + 1); // Lets copy last name.

    // Lets copy it in reverse order.
    strcpy(first_last_name, last_name);
    strcpy(first_last_name + strlen(last_name) + 1, first_name);
    first_last_name[strlen(last_name)] = ',';
}


Share : facebook icon twitter icon
How to overwrite an array of char pointers with a larger list of char pointers?

How to overwrite an array of char pointers with a larger list of char pointers?


By : user1839648
Date : March 29 2020, 07:55 AM
With these it helps argv is an array of pointers of char *. This means that argv has space for argc char * values. If you try to copy more than that many char * values into it, you will end up with an overflow.
Most likely your glob call results in more than argc elements in gl_pathv field (i.e, gl_pathc > argc). This is undefined behavior.
code :
/* Wrong code */
#include <string.h>

int a[] = { 1, 2, 3 };
int b[] = { 1, 2, 3, 4 };
memcpy(a, b, sizeof b);
char **paths = malloc(g.gl_pathc * sizeof *paths);
if (paths == NULL) { /* handle error */ }
for (size_t i=0; i < g.gl_pathc; ++i) {
    /* The following just copies the pointer */
    paths[i] = g.gl_pathv[i];

    /* If you actually want to copy the string, then
       you need to malloc again here.

       Something like:

       paths[i] = malloc(strlen(g.gl_pathv[i] + 1));

       followed by strcpy.
     */
}

/* free all the allocated data when done */
cmd->argv = calloc(g.gl_pathc, sizeof(char *) *g.gl_pathc);
size_t i;
cmd->argv = malloc((g.gl_pathc+g.gl_offs) * sizeof *cmd->argv);
for (i=g.gl_offs; i < g.gl_pathc + g.gl_offs; ++i)
    cmd->argv[i] = strdup(g.gl_pathv[i]);
Why can't one use pointer to char pointers instead of array of char pointers?

Why can't one use pointer to char pointers instead of array of char pointers?


By : Cybelle Burgess
Date : March 29 2020, 07:55 AM
this one helps. Yes. In first case, you have an array of pointers. Each pointer points to a separate item(Alan, Frank...)
The second declaration
code :
char **names;
char *str = "hello"
char **names = &str;
How to split char pointer with multiple delimiters & return array of char pointers in c++?

How to split char pointer with multiple delimiters & return array of char pointers in c++?


By : A'Jain
Date : March 29 2020, 07:55 AM
hop of those help? Wait, what are you talking about? I don't see LPWSTR anywhere in your code. Are you trying to convert to LPWSTR? If so, there's a standard library function for that. There's also a standard library-based solution for splitting over multiple chars. So all together, your code might look like this:
code :
#include <codecvt>
#include <cstdio>
#include <locale>
#include <sstream>
#include <string>

using std::string;
using std::wstring;

wstring toWide(const string &original)
{
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
    return converter.from_bytes(narrow_utf8_source_string);
}

std::vector<wstring> splitMany(const string &original, const string &delimiters)
{
    std::stringstream stream(original);
    std::string line;

    while (std::getline(original, line)) 
    {
        std::size_t prev = 0, pos;
        while ((pos = line.find_first_of(delimeters, prev)) != std::string::npos)
        {
            if (pos > prev)
                wordVector.push_back(line.substr(prev, pos-prev));
            prev = pos + 1;
        }
        if (prev < line.length())
            wordVector.push_back(line.substr(prev, std::string::npos));
    }
}

int main()
{
    string original = "This:is\nmy:tst?why I hate";
    string separators = ":? \n"

    std::vector<wstring> results = splitMany(original, separators);
}
#include <codecvt>
#include <locale>
#include <string>

using std::string;
using std::wstring;

string toMultiByte(const wstring &original)
{
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
    return converter.to_bytes(original);
}
How can you tell whether a variable is a 2D array, array of pointers or double pointers of char?

How can you tell whether a variable is a 2D array, array of pointers or double pointers of char?


By : Diogo Montagner
Date : March 29 2020, 07:55 AM
Hope this helps
How can you tell whether a variable is a 2D array, array of pointers or double pointers of char?
code :
#define xtype(X) _Generic((&X), \
    char (*)[4][4]: "char [4][4]", \
    char *(*)[4]  : "char *[4]", \
    char ***      : "char **", \
    char (*)[4]   : "char [4]", \
    char **       : "char *", \
    char *        : "char", \
    default       : "?" \
)

int main(void) {
  char a[4][4];
  char *b[4];
  char **c;
  puts(xtype(a));
  puts(xtype(b));
  puts(xtype(c));
  puts(xtype(a[0]));
  puts(xtype(b[0]));
  puts(xtype(c[0]));
  puts(xtype(a[0][0]));
  puts(xtype(b[0][0]));
  puts(xtype(c[0][0]));
}
char [4][4]
char *[4]
char **
char [4]
char *
char *
char
char
char
#define xtype(X) _Generic((&X), \
    char (*)[][4] : "char [][4]", \
    char *(*)[]   : "char *[]", \
    char ***      : "char **", \
    char (*)[]    : "char []", \
    char **       : "char *", \
    char *        : "char", \
    default       : "?" \
)
How to declare and initialize an array of pointers to array of pointers to char?

How to declare and initialize an array of pointers to array of pointers to char?


By : user2727940
Date : March 29 2020, 07:55 AM
hope this fix your issue I want to declare an array of pointers to array of pointers to char and initialize it. But i couldn't declare and initialize it using the following way: , Before C99, you had to define the inner arrays separately:
code :
#include <stdio.h>

int main() {
    const char *array_0[] = { "car", "toy", "game" };
    const char *array_1[] = { "go", "play", "read" };
    const char **array[] = { array_0, array_1 };
    int i, j;

    for (i = 0; i < 2; i++) {
        for (j = 0; j < 3; j++) {
            printf("%s ", array[i][j]);
        }
        printf("\n");
    }
    return 0;
}
#include <stdio.h>

int main() {
    const char **array[] = {
        (const char *[]){ "car", "toy", "game" },
        (const char *[]){ "go", "play", "read" },
    };

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%s ", array[i][j]);
        }
        printf("\n");
    }
    return 0;
}
#include <stdio.h>

int main() {
    const char **array[] = {
        (const char *[]){ "car", "toy", "game", NULL },
        (const char *[]){ "go", "play", NULL },
        (const char *[]){ "read", NULL },
        NULL,
    };

    for (int i = 0; array[i]; i++) {
        for (int j = 0; array[i][j]; j++) {
            printf("%s ", array[i][j]);
        }
        printf("\n");
    }
    return 0;
}
car toy game 
go play 
read
Related Posts Related Posts :
  • Referring to a CMAKE variable from code
  • Convert a CString to an array of floats
  • Generating a sequence of zeros at compile time
  • Pass std::iterator pointing to a vector of Eigen types
  • Overloaded pointer to function
  • How to visualise a simple std::string with natvis?
  • OpenCV: can't access Mat elements
  • What happens when you redefine a macro?
  • use the TYPE from the tempate type argument list
  • Warning: Multiple copy constructors defined while deleting them
  • xgboost build error on CentOS 6.8
  • How can I sort elements of a vector by row using std::sort
  • Using an std::function for wrapping a function object
  • How to add a Combobox or browse option in one particular row of a grid control using CMFCPropertyGridProperty class
  • update curl on centos
  • How can I use Linguist in Qt without .pro file?
  • reduce data to be sent over UDP sockets
  • cudaModuleLoadData fails with error code 201
  • C++ code not compiling with gcc on Mac?
  • Editing the value in an unordered map for a given key
  • How do call C++ function through Delphi wrapper
  • How could I link the Boost (not in usr/include) in CMakeList.txt using find_package
  • C++, Linker Issue returning undefined reference
  • Can you use std::string in a 3rd party dll?
  • Why does this use of std::sort with a custom comparator not compile?
  • Fixed base address in MSVC2015
  • Armadillo C++ Matrix Library - simplest way to initialize all values in a matrix to 999?
  • How do you end while loop
  • C++ Basic While Loop: Unknown Inputs and Value Increment
  • Cmake build failed with CMAKE_AR-NOTFOUND (cr exe not found)
  • c++ typeid on class member operator() overloads
  • Different size of string showing when going by two different methods
  • what the meaning "source of entropy of sth." on stack
  • wxWidgets - wxStyledTextCtrl - Text alignment RTL
  • get the entry point to a 64bit process memory from a 32bit process
  • Stroustrup: For C++, how do I install FLTK library?
  • Getting a word after a certain character?
  • How can I convert a char vector to a char array?
  • Comparing vectors of different length c++
  • How do I add four seperate times together?
  • How to replace data in a certain node in a linked list?
  • Prepared statements in C++ libneo4j_client?
  • FindFirstVolume does not return EFI system partition
  • User Inputs Value, but Arbitrary Value Printed to Screen. Printed Value doesn't Match Entered Value
  • How to work with std::make_unique function and interface classes?
  • Refresh image in command line (C++)
  • Outputting time in minutes and seconds C++
  • Creating a for loop to continue to count
  • how to get to field of 2D object dynamic array?
  • Multiple class object share common variable
  • Why is my multi-threaded program not terminating properly?
  • How do I open a file in C++ using just the filename?
  • QT DOMXml - Change the name of a node
  • Using undeclared list as argument in c++
  • While loop won't continue
  • If/else if always goes to else statement
  • Why shared_timed_mutex is defined in c++14, but shared_mutex in c++17?
  • Confused by pipes. 'cat -A' seems to filter out part of output
  • Cannot open source file gtest/gtest.h
  • How to get a certain word in string of getline?
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com