CStrings, Passing to function and manipulating string?

This is a discussion on CStrings, Passing to function and manipulating string? within the C Programming forums, part of the General Programming Boards category; Hi guys. I've got a frustrating problem that has seriously been puzzling me for a week. Basically, I want my ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Location
    Birmingham, UK.
    Posts
    14

    CStrings, Passing to function and manipulating string?

    Hi guys.

    I've got a frustrating problem that has seriously been puzzling me for a week.

    Basically, I want my function to change the text within the CString. Here is an example:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void change(char buffer[]) {
      strcpy(buffer, "FoobBar\n");
    }
    
    int main(int argc, char *argv[]) {
    
        char  buffer[20] = "Example";
        printf("%s\n",buffer);        // Prints example
        change(buffer);               
        printf("%s\n",buffer);        // Should print FooBar
    
      return 0;
    }
    Or

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void change(char *buffer) {
      strcpy(buffer, "FoobBar\n");
    }
    
    int main(int argc, char *argv[]) {
    
        char  buffer[20] = "Example";
        printf("%s\n",buffer);        // Prints example
        change(buffer);               
        printf("%s\n",buffer);        // Should print FooBar
    
      return 0;
    }
    I have a feeling it's something to do with buffer[20] being stored in part of the memory I donot have access to. Any ideas on how to solve this?

    Am I doing it the correct way?
    Last edited by jamesjeffery; 08-15-2009 at 07:25 AM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    Both examples are correct. Is it not working in some way? Did you try it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Location
    Birmingham, UK.
    Posts
    14
    Quote Originally Posted by Elysia View Post
    Both examples are correct. Is it not working in some way? Did you try it?
    Ah, so it does. Sorry. What I did was shorten the C file, to show an example of what I was doing. The original file don't work.

    Here is what I was trying to do.

    Code:
    #include <stdio.h>
    #include <curl/curl.h>
    #include <string.h>
    
    size_t write_data(char *ptr, size_t size, size_t nmemb, char buffer[]) {
      strcpy(buffer, ptr);
      printf("%s", buffer);   // Should print out the HTML source
      return (size * nmemb);
    }
    
    int main(int argc, char *argv[]) {
    
        char  *username       = argv[1];
        char  *password       = argv[2];
        char  *url            = argv[3];
        int   startRange      = atoi(argv[4]);
        int   endRange        = atoi(argv[5]);
        char  buffer[1000];
        
        CURL *curl = curl_easy_init();
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_easy_setopt(curl, CURLOPT_REFERER, "http://www.yahoo.com");
        curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION , &write_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, buffer);       
        curl_easy_perform(curl);
        curl_easy_cleanup(curl);
    
        printf("%s", buffer);   // Should hold the HTML source
    
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Dec 2008
    Location
    Birmingham, UK.
    Posts
    14
    Ah, solved it. The memory set aside for buffer was to small.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manipulating a passed pointer inside a function.
    By see_c in forum C Programming
    Replies: 12
    Last Post: 05-25-2006, 02:35 PM
  2. Manipulating a string
    By pabellon in forum C Programming
    Replies: 1
    Last Post: 12-19-2001, 05:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21