Dynamically stored strings. What is the exact method?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Avon
    New Member
    • May 2007
    • 18

    Dynamically stored strings. What is the exact method?

    Hello there.

    I'm (kind of) new to programming as you can see, and I have a question.

    I want the user to input a string (for example: a product code) but store it dynamically. There must be an other way instead of a static array with, let's say, 50 elements. What I have been trying is this:

    Code:
    	int c;
     	char *w, test;
     	w=&test;
    	printf("Give product code: ");
    	while (c = getchar() != EOF)
    		{
    		*w=c;
    		w=w+1;
    		}
      	w=w+1;
       	*w='\0';
    
    	
    	printf("%s", *w);
    However, I always seem to get (null) as the printf result. I even tryed setting a counter and printing the *(w-counter) result but yet again... no luck. Any help really appreciated. :)

    (By the way, on a side note. Is there any way I can avoid the declaration of "test" and "w=&test"?)

    Thank you very much. :)
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by Avon
    Hello there.

    I'm (kind of) new to programming as you can see, and I have a question.

    I want the user to input a string (for example: a product code) but store it dynamically. There must be an other way instead of a static array with, let's say, 50 elements. What I have been trying is this:

    Code:
    	int c;
     	char *w, test;
     	w=&test;
    	printf("Give product code: ");
    	while (c = getchar() != EOF)
    		{
    		*w=c;
    		w=w+1;
    		}
      	w=w+1;
       	*w='\0';
    
    	
    	printf("%s", *w);
    However, I always seem to get (null) as the printf result. I even tryed setting a counter and printing the *(w-counter) result but yet again... no luck. Any help really appreciated. :)

    (By the way, on a side note. Is there any way I can avoid the declaration of "test" and "w=&test"?)

    Thank you very much. :)
    And how did u exectly used the counter?

    Savage

    Comment

    • Avon
      New Member
      • May 2007
      • 18

      #3
      Well I added the counter inside the "while" loop but I didn't have high hopes anyway. ;) Here is what it looked like.

      Code:
              int c,counter=0;
       	char *w, test;
       	w=&test;
      	printf("Give product code: ");
      	while (c = getchar() != EOF)
      		{
      		*w=c;
      		w=w+1;
                      counter=counter+1;
      		}
        	w=w+1;
         	*w='\0';
      
      	
      	printf("%s", *(w-counter));

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        Originally posted by Avon
        Well I added the counter inside the "while" loop but I didn't have high hopes anyway. ;) Here is what it looked like.

        Code:
                int c,counter=0;
         	char *w, test;
         	w=&test;
        	printf("Give product code: ");
        	while (c = getchar() != EOF)
        		{
        		*w=c;
        		w=w+1;
                        counter=counter+1;
        		}
          	w=w+1;
           	*w='\0';
        
        	
        	printf("%s", *(w-counter));
        But,have u tryed this:

        Code:
        	while (c = getchar() != EOF)
        		{
        		   *(w+counter)=c;
                                           counter=counter+1;
                                        }
        ???

        Savage

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          THis code:
          Code:
          int c,counter=0;
           	char *w, test;
           	w=&test;
          	printf("Give product code: ");
          	while (c = getchar() != EOF)
          		{
          		*w=c;
          		w=w+1;
                          counter=counter+1;
          		}
            	w=w+1;
             	*w='\0';
          
          	
          	printf("%s", *(w-counter));
          Has some problems. First, test is 1 char. w is a pointer to that char. That means you can w=w+1 becuse you don't own the location w+1.

          You will need to build an array. Check out strcat().

          Comment

          • Avon
            New Member
            • May 2007
            • 18

            #6
            Originally posted by weaknessforcats
            THis code:
            Code:
            int c,counter=0;
             	char *w, test;
             	w=&test;
            	printf("Give product code: ");
            	while (c = getchar() != EOF)
            		{
            		*w=c;
            		w=w+1;
                            counter=counter+1;
            		}
              	w=w+1;
               	*w='\0';
            
            	
            	printf("%s", *(w-counter));
            Has some problems. First, test is 1 char. w is a pointer to that char. That means you can w=w+1 becuse you don't own the location w+1.

            You will need to build an array. Check out strcat().
            Ok thanks for the reply. I understand what the problem is, however, I don't unterstand how to use the array. Am I going to use it in a way that it stores each separate string until you call the function again, or store all strings? If you mean the second, how do we know the size of the array? :)

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              To store an array of 10 strings you need an array of char*. Either:
              Code:
              char* arr[10];
              or
              Code:
              char** arr = new char*[10];
              These two declaratiions are equivalent. The only difference is that you have to delete the array you allocate with new.

              So, now you have an array of char*.

              Get your string into a buffer.

              Now just allocate memory of the right amount and attach it to one of your char* array elements. Then copy the string to your memory allocation:

              Code:
              arr[i] = new char[strlen(buffer) + 1];  //+1 for the null terminator
              strcpy(arr[i], buffer;
              and off you go.

              Comment

              Working...