Thread: The pointer in my function is failing my assumption: Exercise 13 Chapter 13

  1. #1
    Registered User
    Join Date
    Aug 2022
    Posts
    40

    The pointer in my function is failing my assumption: Exercise 13 Chapter 13

    Hello,

    I'm working on a problem and I'm stuck.
    <<< BEGIN QUESTION >>>
    Write the following function:
    Code:
    void build_index_url(const char *domain, char *index_url);
    domain points to a string containing an Internet domain, such as "knking.com". The function should add "http://www." to the beginning of this string and "/index_html" to the end of the string, storing the result in the string pointed to by index_url. (In this example, the result will be "http://www.knking.com/index.html".) You may assume that index_url points to a variable that is long enough to hold the resulting string. Keep the function as simple as possibleby having it use the strcat and strcpy functions.
    <<< END QUESTION >>>

    Code:
    #include <stdio.h>
    #include <string.h>
    // http://knking.com/books/c2/answers/c13.html
    
    
    void build_index_url(const char *domain, char *index_url);
    
    
    int main(){
        const char dom[]="knking.com";
        char url[50];
        build_index_url(dom, url);
        printf("url = %s\n", url);  // prints: url = 
        return (0);
    }
    
    
    void build_index_url(const char *domain, char *index_url){
        char preBuild[20]="http://www.";
        char postBuild[20]="/index.html";
        char newBuild[20];
        strcat(preBuild, domain);
        strcat(preBuild, postBuild);
        strcpy(index_url, preBuild);    // put data in location pointed to by index_url
        printf("function: index_url = %s\n", index_url);  // works fine
    }
    In the build_index_url function, I assumed that index_url was pointing to url as it was defined in main(). The problem: when the program returns to main(), I can't get url to print "http://www.knking.com/index.html". What an I not understanding?

  2. #2
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    I wonder if you are experiencing problems with your stack variable:

    Code:
    char url[50];
    What does the character array, url, contain? i.e. Is url a c string like strcpy requires?

    Actually I tried changing the sizes of these two arrays and everything works:

    Code:
    char preBuild[50]="http://www.";
    char postBuild[50]="/index.html";
    Last edited by G4143; 12-06-2022 at 08:03 PM.

  3. #3
    Registered User
    Join Date
    Aug 2022
    Posts
    40
    Initially I 'think' char url[50] contains '\0' because I haven't put anything into it.
    At the end of build_index_url() I am trying give url a value of "http://www.knking.com/index.html" by using strcpy(index_url, preBuild);
    index_url is a pointer to url.

  4. #4
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    I think you find stack variables contain whatever is in memory(that the stack uses) unless you initialize that stack variable.

  5. #5
    Registered User
    Join Date
    Aug 2022
    Posts
    40
    Sorry, I get it now.

    Code:
    char preBuild[50]="http://www.";
    char postBuild[50]="/index.html";
    THANK YOU!
    Last edited by mpatters; 12-06-2022 at 08:25 PM. Reason: oversight. I didn't see the [50] that was suggested.

  6. #6
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    I'd also check that stack variable:

    Code:
    char url[50];
    and make sure you don't have to initialize it to:
    Code:
    char url[50] = "\0";

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You shouldn't need to modify your fixed strings at all.
    Code:
        const char preBuild[]="http://www.";
        const char postBuild[]="/index.html";
        strcpy(index_url, preBuild);   
        strcat(index_url, domain);
        strcat(index_url, postBuild);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Aug 2022
    Posts
    40
    I put it back to charurl[50]; and it works fine. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-11-2016, 05:50 AM
  2. Replies: 3
    Last Post: 08-04-2016, 05:35 AM
  3. Jumping into C++ Chapter 14, exercise 6.
    By CppProgrammer88 in forum C++ Programming
    Replies: 5
    Last Post: 04-12-2016, 07:27 PM
  4. Alex's exercise Chapter 6 Ex3
    By Valentas in forum C++ Programming
    Replies: 3
    Last Post: 01-25-2013, 10:55 AM

Tags for this Thread