473,414 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Dynamically stored strings. What is the exact method?

18
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:

Expand|Select|Wrap|Line Numbers
  1.     int c;
  2.      char *w, test;
  3.      w=&test;
  4.     printf("Give product code: ");
  5.     while (c = getchar() != EOF)
  6.         {
  7.         *w=c;
  8.         w=w+1;
  9.         }
  10.       w=w+1;
  11.        *w='\0';
  12.  
  13.  
  14.     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. :)
May 7 '07 #1
6 1796
Savage
1,764 Expert 1GB
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:

Expand|Select|Wrap|Line Numbers
  1.     int c;
  2.      char *w, test;
  3.      w=&test;
  4.     printf("Give product code: ");
  5.     while (c = getchar() != EOF)
  6.         {
  7.         *w=c;
  8.         w=w+1;
  9.         }
  10.       w=w+1;
  11.        *w='\0';
  12.  
  13.  
  14.     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
May 7 '07 #2
Avon
18
Well I added the counter inside the "while" loop but I didn't have high hopes anyway. ;) Here is what it looked like.

Expand|Select|Wrap|Line Numbers
  1.         int c,counter=0;
  2.      char *w, test;
  3.      w=&test;
  4.     printf("Give product code: ");
  5.     while (c = getchar() != EOF)
  6.         {
  7.         *w=c;
  8.         w=w+1;
  9.                 counter=counter+1;
  10.         }
  11.       w=w+1;
  12.        *w='\0';
  13.  
  14.  
  15.     printf("%s", *(w-counter));
May 7 '07 #3
Savage
1,764 Expert 1GB
Well I added the counter inside the "while" loop but I didn't have high hopes anyway. ;) Here is what it looked like.

Expand|Select|Wrap|Line Numbers
  1.         int c,counter=0;
  2.      char *w, test;
  3.      w=&test;
  4.     printf("Give product code: ");
  5.     while (c = getchar() != EOF)
  6.         {
  7.         *w=c;
  8.         w=w+1;
  9.                 counter=counter+1;
  10.         }
  11.       w=w+1;
  12.        *w='\0';
  13.  
  14.  
  15.     printf("%s", *(w-counter));
But,have u tryed this:

Expand|Select|Wrap|Line Numbers
  1.     while (c = getchar() != EOF)
  2.         {
  3.            *(w+counter)=c;
  4.                                    counter=counter+1;
  5.                                 }
???

Savage
May 7 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
THis code:
Expand|Select|Wrap|Line Numbers
  1. int c,counter=0;
  2.      char *w, test;
  3.      w=&test;
  4.     printf("Give product code: ");
  5.     while (c = getchar() != EOF)
  6.         {
  7.         *w=c;
  8.         w=w+1;
  9.                 counter=counter+1;
  10.         }
  11.       w=w+1;
  12.        *w='\0';
  13.  
  14.  
  15.     printf("%s", *(w-counter));
  16.  
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().
May 7 '07 #5
Avon
18
THis code:
Expand|Select|Wrap|Line Numbers
  1. int c,counter=0;
  2.      char *w, test;
  3.      w=&test;
  4.     printf("Give product code: ");
  5.     while (c = getchar() != EOF)
  6.         {
  7.         *w=c;
  8.         w=w+1;
  9.                 counter=counter+1;
  10.         }
  11.       w=w+1;
  12.        *w='\0';
  13.  
  14.  
  15.     printf("%s", *(w-counter));
  16.  
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? :)
May 7 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
To store an array of 10 strings you need an array of char*. Either:
Expand|Select|Wrap|Line Numbers
  1. char* arr[10];
  2.  
or
Expand|Select|Wrap|Line Numbers
  1. char** arr = new char*[10];
  2.  
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:

Expand|Select|Wrap|Line Numbers
  1. arr[i] = new char[strlen(buffer) + 1];  //+1 for the null terminator
  2. strcpy(arr[i], buffer;
  3.  
and off you go.
May 8 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: ‘5ÛHH575-UAZWKVVP-7H2H48V3 | last post by:
(see end of message for example code) When an instance has a dynamically assigned instance method, deepcopy throws a TypeError with the message "TypeError: instancemethod expected at least 2...
8
by: Falc2199 | last post by:
Hi, Does anyone know how to make this work? var sectionId = 5; repeat_section_sectionId(); function repeat_section_5(){ alert("firing"); }
6
by: Thomas | last post by:
Hi, I'm having a problem with the dynamically created inputfields in Internet Explorer. The situation is the following: - I have a dynamically created table with a textbox in each Cell. - It...
3
by: Samarth | last post by:
Folks, I am calling a DB2 stored procedure through Perl using the DBI:ODBC module. I am not sure if I can do this or not because I have been able to connect to and also issue select statements...
48
by: Michel Rouzic | last post by:
I know it must sound like a newbie question, but I never really had to bother with that before, and I didn't even find an answer in the c.l.c FAQ I'd like to know what's the really proper way...
4
by: jesper_lofgren | last post by:
Hi there, I have some webcontrols that i want to add dynamically on a page. I have stored the path / namespace in database (ex MyNameSpace.WebControls.Control1) to the class/webcontrol. Lets...
37
by: sam44 | last post by:
Hi, At startup the user log on and chooses the name of a client from a dropdownlist, which then changes dynamically the connection string (the name of the client indicates which database to use)....
6
by: AlabiChin | last post by:
Hello, I'm trying to find out if it is possible to dynamically add or remove fields for a structure. I'm in a situation where I don't know how many items I want to store and, therefore, will not...
3
by: Tod Birdsall | last post by:
Hi, My boss has asked me to create a tool that can give us a list of stored procedure names that are being called from each of our app's web pages. This is an ASP.NET application that is rather...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.