Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7843513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:34:03+00:00 2026-06-02T16:34:03+00:00

I’m trying to return a data structure from a function named urlTokener where in

  • 0

I’m trying to return a data structure from a function named urlTokener where in the data structure one member is an array of type char* and the other is int. When I print the values of the array in the function urltokener I get the correct type, but I see that in the main function where the returned data structure is used, the array doesn’t contain the right values as the output is not right(not the same as in the function). It seems the function is not returning the data structure correctly. Could you please check and say what I’m doing wrong in the following code?

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

struct tokenDetail
{
    char* theArray[256];

    int sizeOfArray;
};


tokenDetail urlTokener(const char *,char* );

void main()
{
    // String to be splitted.
    const char* url="/v1/AUTH_abb52a71-fc76-489b-b56b-732b66bf50b1/images?limit=1000&delimiter=/&format=xml"  ;

    tokenDetail newdetails;
    newdetails=urlTokener(url,"?");
    for (int i=0;i<newdetails.sizeOfArray;i++)
    {
        printf("This is in main where size is %d  and the value %s\n",newdetails.sizeOfArray,newdetails.theArray[i]);
    }
}

tokenDetail urlTokener(const char* urlLine,char* delimiter)
{
    char urlArray[256];
    strncpy(urlArray, urlLine, sizeof(urlArray));
    tokenDetail details;
    unsigned int index = 0;
    details.theArray[index] = strtok(urlArray, delimiter);

    while(details.theArray[index] != 0)
    {
        printf("This is in function %s\n",details.theArray[index]);
        ++index;
        details.theArray[index] = strtok(0, delimiter);
    }
    for (int i=0;i<index;i++)
    {
        printf("This is in function 2nd time %s\n",details.theArray[i]);
    }
    details.sizeOfArray=index;
    return details;
}

Please note : I’m doing it for c++, but I’m not allowed(by the person who gave me this task) to use namespace std, and library string. Which is why the code is similar to C. Because of this limitation I’m confused whether to tag it C or C++. So I tagged it in both c and c++. May be you could decide yourself.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:34:04+00:00Added an answer on June 2, 2026 at 4:34 pm

    You’re filling the array with pointers returned from strtok, which in
    turn are pointers into your urlArray, a local variable, which ceases
    to exist once you return from the function.

    Is this C or C++? You’ve set both labels, and the solution is
    different. In C++, the obvious solution is to replace the char* in
    tokenDetail with std::string. In C, it’s a bit more complicated.
    You have dynamically allocate the strings in your struct (using the
    non-standard but widely available strdup), and require the client to
    free them. The usual solution here is to return a pointer to a
    dynamically allocated TokenDetail, and a freeTokenDetail function
    which the client is required to call on the returned pointer. This
    gives you total freedom in how you do your allocations and frees. (The
    usual solution is, in fact, to only provide a forward declaration of
    TokenDetail to the clients, and to provide functions which they have
    to call to access its elements.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to construct a data frame in an Rcpp function, but when I
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.