Thread: String allocation to string pointer

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    6

    String allocation to string pointer

    Team,

    I am trying to read the string from user and the allocate it to the another string which is ptr string but not successful, so not sure what am I missing here.

    Do I have to use any dynamic memory allocation here?
    Code:
    int main(){
    
        char test[5];
        char *strng, *base;
        int i;
    
        base=strng;
        for(i=0; i<4; i++){
    
    printf("Enter the value of element %d: ",i);
            scanf("%s",test);
            strng=test;   <<<<<< is this valid?
            strng++; <<< want to store value of test to strng and then move strng to next block so I can store new value.
    
        }
        
    //Following is to print the value that I stored from test to strng.
        strng= base;
        for(i=0; i<4; i++){
    printf("value of strng is %s\n", strng);
            strng++;
        }
    
    
    }
    
    

    It will be great if you can point me to any reference or doc to read about it.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Read the link on Pointers Pointers in C - Tutorial - Cprogramming.com

    You are missing what a pointer is in C; what you are doing is NOT correct or safe to do.

    I have decided that Pointers are beyond my ability to explain it to anyone.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    6
    So what will be corrected code snip?

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    It's hard to tell what you're trying to do.

    If you want to keep all the user input separate then you probably want an array of arrays. char arr2d[4][5]; is a 2d array with 4 rows and 5 columns (essentially).

    If you want to concatenate user input then you can just allocate one string. char strng[20]; is an array of 20 characters. You can then copy each input into the end of whatever you have in the array.

    Also you cannot use = to copy strings (unless you know what you're doing). Use strcpy or preferably strncpy.

    You should also try not to use magic numbers and your code is vulnerable to buffer overflow and whatnot but it's probably more important that you properly learn arrays and pointers right now.

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    6
    Hi DeadPlanet,

    Actually I am trying to read the name in string array test and then trying to copy the same elements in character array strng but its not working. As strng is pointer so I should be able to do strng = test in each iteration of first for loop. However I am not able to achieve it. I want to initialize the strng using pointer so I dont have to worry about how many elements and space. Is malloc is the way to go with strng[i][j]? where i will be number of row of string and j will be base of specific row string array's first char?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by devbegin View Post
    character array strng but its not working. As strng is pointer
    This is self-contradictory. Either strng is a character array or a pointer, but not both. If you want a copy, then using a pointer for strng is incorrect, as that wouldn't give you a copy (it would merely give you another name for the same string in memory).

    You also seem to think you have more than one string, but you do not. You have one string that can hold up to four characters.

    Quote Originally Posted by devbegin View Post
    so I dont have to worry about how many elements and space
    This is not an option in C. It is your job as the programmer to allocate the memory you need for storage.

  7. #7
    Registered User
    Join Date
    Feb 2014
    Posts
    6
    HI Tabstop,

    so basically I will have to do sizeof or strlen to find size of it and allocate the space for strng using malloc, that will store one of the string, how about the second string I have, I will have to do same malloc action and assign second string to it, but how will I keep track of the from where first string is stored in strng and how I navigate to all strings which I want to store in strng.

    I want to do something like:
    strng[0]=abc
    strng[1]=abcd

    using malloc I will allocate the space for each string but how will I keep track of where those are store and how can I access them using strng[1]?

    should I do something like

    strng[0][malloc(sizeof(test))]
    strng[1][malloc(sizeof(test))]

  8. #8
    Registered User
    Join Date
    Feb 2014
    Posts
    6
    This is what I tried for single string:

    Code:
    int main(){
            char *s1;
            char s2;
            int a;
    
    
            struct utmp ut;
    
    
            a = sizeof(ut.ut_line);
            printf ("Size of line is %d\n",a);
    
    
            s1 = malloc(a);
            s1 = "Test";
            printf ("size of s1 is %d\n", sizeof(s1));
            printf("Value of s1 is %s and size is %d\n", s1, sizeof(s1));
    }
    [root@ c_prog]# ./a.out 
    Size of line is 32
    size of s1 is 8
    Value of s1 is Test and size is 8
    [root@ c_prog]#
    I used the sizeof to find the size of ut_line which is 32 but s1 is allocated with only 8?

  9. #9
    Registered User
    Join Date
    Feb 2014
    Posts
    6
    Code:
    int main(){
            char *s1;
            char s2;
            int a;
    
    
            struct utmp ut;
    
    
            a = sizeof(ut.ut_line);
            printf ("Size of line is %d\n",a);
    
    
            s1 = malloc(a);
            s1 = "Test is test, what do you say";
            printf ("size of s1 is %d\n", sizeof(s1));
            printf("Value of s1 is %s and size is %d\n", s1, sizeof(s1));
    }
    I tried with bigger string but it still writes to it, should it not restrict to 8?

    ./a.out
    Size of line is 32
    size of s1 is 8
    Value of s1 is Test is test, what do you say and size is 8

    Value I gave is "Test is test, what do you say" and it took it whole.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-15-2012, 06:09 AM
  2. C/C++ string allocation question
    By donglee in forum C++ Programming
    Replies: 7
    Last Post: 04-28-2009, 12:16 PM
  3. string vector to string pointer manipulation
    By stanlvw in forum C++ Programming
    Replies: 11
    Last Post: 07-16-2008, 01:43 AM
  4. Memory Allocation Using String (Please Help)
    By kolliash in forum C Programming
    Replies: 5
    Last Post: 05-06-2008, 03:32 AM
  5. String allocation
    By fw9189 in forum C Programming
    Replies: 5
    Last Post: 10-06-2001, 11:07 AM