Thread: Simple string question

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    6

    Simple string question

    Here is my code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void sayHi(char *);
    
    int main()
    {
    	char *ptr;
    	sayHi(ptr);
    	printf("prt: %s\n", ptr);
    	return 0;
    }
    
    void sayHi(char *str)
    {
    	char *temp = "hello\0";
    	str = temp;
    	printf("str: %s\n", str);
    }
    I am trying to assign the value "hello" to the pointer I created in main. In function sayHi, the string prints correctly after the pointer assignment. However, it doesn't print correctly in main. Please help.

    Thanks

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    87
    That is because a "string" in C is just an array of characters. A char * is a pointer to a character - that is a variable that stores a memory address (the address where the first character is). The line
    Code:
    str = temp
    is setting the memory location pointed to by str equal to the memory location pointed to by temp. This is fine inside your function. But when you exit your function, the variables - like temp - that were local to that function are no longer around (or at least you cannot rely on them). This the memory address pointed to by str is now garbage.

    Do this:
    First, allocate space for str using malloc(). Something like str = malloc(n) where n is how many characters long (including a null terminating character) you want the string to be.
    Next use strncpy() to copy the characters pointed to by temp into the memory pointed to by str.

    Hope that helps

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    A local copy of ptr is passed to sayHi, if you want to modify the value of ptr in main, you need to pass a pointer to ptr, so you would have void sayHi(char **str).

    jason_m: you're thinking of the wrong problem, though your point about temp is right.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    Thanks for the replies. I tried malloc() but the problem still persists. Doodle, what you are saying makes sense, I am just having trouble implementing it correctly. I know I'm still doing something wrong; here are the changes I made. I just changed the function sayHi to accept char **.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void sayHi(char **);
    
    int main()
    {
    	char *ptr;
    	sayHi(ptr);
    	printf("prt: %s\n", ptr);
    	return 0;
    }
    
    void sayHi(char **str)
    {
    	char *temp = "hello\0";
    	str = malloc(10);
    	strncpy(str, temp, 10);
    	printf("str: %s\n", str);
    }

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    87
    Ah yes, I was mistaken as to what the problem was. If you are still seg faulting my guess is that you didn't allocate space for the pointer to your character pointer.

    Something like this should do:

    Code:
    void sayHi(char **str) {
      ...
      strncpy(*str, temp, 6) /* copy the characters pointed to by temp into the ones pointed to by *str - up to a maximum of 6 - we don't want any buffer overflows around here */
      ...
    }
    
    int main() {
      char **str;
    
      str = malloc(sizeof(char *)); /* allocate space for the pointer to the pointer to the character */
      *str = malloc(6 * sizeof(char)); /* allocate space for the string itself - you could also do this in sayHi */
      sayHi(str);
      ...
    }
    That "*" before str is "dereferencing" the pointer. This is how you get at the content stored at the memory address. So,
    str = foo means set the memory address pointed to by str equal to foo.
    *str = foo means set the contents located at the memory address pointed to by str equal to foo.

    I think I got it this time, hope that helps.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    Thanks a lot, I got it! I spend all day on this stupid problem. I had to change a few things in your code so here is the working version if anyone else cares.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void sayHi(char **);
    
    int main()
    {
    	char **str;
    	str = malloc(sizeof(char *));     /* allocate space for the pointer to the pointer to the character */
    
    	*str = malloc(6 * sizeof(char)); /* allocate space for the string itself - you could also do this in sayHi */
    
    	sayHi(&str);
    	printf("srt: %s\n", str);
    	return 0;
    }
    
    void sayHi(char **str)
    {
    	char *temp = "hello\0";
    	strncpy(*str, temp, 6);  /* copy the characters pointed to by temp into the ones pointed to by *str - up to a maximum of 6 - we don't want any buffer overflows around here */
    
    	printf("str: %s\n", *str);
    }

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    free() what you malloc().

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    Ok, more questions. I am getting the following warnings:

    test.c: In function ‘main’:
    test.c:9: warning: incompatible implicit declaration of built-in function ‘malloc’
    test.c:10: warning: passing argument 1 of ‘sayHi’ from incompatible pointer type
    test.c: In function ‘sayHi’:
    test.c:19: warning: incompatible implicit declaration of built-in function ‘malloc’

    Here is the code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void sayHi(char **);
    
    int main()
    {
    	char **str;
    	str = malloc(sizeof(char *));
    	sayHi(&str);
    	printf("srt: &#37;s\n", str);
    	free(str);
    	return 0;
    }
    
    void sayHi(char **str)
    {
    	char *temp = "hello\0";
    	*str = malloc(6 * sizeof(char));
    	strncpy(*str, temp, 6);
    	printf("str: %s\n", *str);
    }
    Thanks for the feedback, you guys are life savers!

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    > *str = malloc(6 * sizeof(char));
    http://cboard.cprogramming.com/showp...45&postcount=7

    #include <stdlib.h>

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    6
    Thanks rob, I missed that other one. I'm still getting these warning messages though. I assume it is because the formal parameters are type char **, and I am the actual parameter I am passing is &char**. Anyone have any ideas?

    Warnings:
    test.c: In function ‘main’:
    test.c:9: warning: incompatible implicit declaration of built-in function ‘malloc’
    test.c:10: warning: passing argument 1 of ‘sayHi’ from incompatible pointer type
    test.c: In function ‘sayHi’:
    test.c:20: warning: incompatible implicit declaration of built-in function ‘malloc’

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void sayHi(char **);
    
    int main()
    {
    	char **str;
    	str = malloc(sizeof(char *));
    	sayHi(&str);
    	printf("srt: %s\n", str);
    	free(str);
    	free(*str);
    	return 0;
    }
    
    void sayHi(char **str)
    {
    	char *temp = "hello\0";
    	*str = malloc(6 * sizeof(char));
    	strncpy(*str, temp, 6);
    	printf("str: %s\n", *str);
    }

  11. #11
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Here.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void sayHi(char **);
    
    int main()
    {
    	char *str;
    	sayHi(&str);
    	printf("str: %s\n", str);
    	
    	free(str);
    	return 0;
    }
    
    void sayHi(char **str)
    {
    	*str = malloc(6 * sizeof(char));
    	if (!(*str)) return;
    	strcpy(*str, "Hello");
            return;
    }

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by gareth00 View Post
    Warnings:
    test.c: In function ‘main’:
    test.c:9: warning: incompatible implicit declaration of built-in function ‘malloc’
    test.c:10: warning: passing argument 1 of ‘sayHi’ from incompatible pointer type
    test.c: In function ‘sayHi’:
    test.c:20: warning: incompatible implicit declaration of built-in function ‘malloc’
    #include <stdlib.h>

  13. #13
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Use my code and add #include <stdlib.h>, I just copy and pasted his, edited, and didn't compile so I didn't notice that the include was missing. My mistake.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Simple string question?
    By Cstudent2121 in forum C Programming
    Replies: 7
    Last Post: 12-03-2005, 11:47 PM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM