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: - 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. :)
6 1797
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: - 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
Well I added the counter inside the "while" loop but I didn't have high hopes anyway. ;) Here is what it looked like. -
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));
Well I added the counter inside the "while" loop but I didn't have high hopes anyway. ;) Here is what it looked like. -
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: - while (c = getchar() != EOF)
-
{
-
*(w+counter)=c;
-
counter=counter+1;
-
}
???
Savage
THis 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().
THis 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? :)
To store an array of 10 strings you need an array of char*. Either:
or -
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: -
arr[i] = new char[strlen(buffer) + 1]; //+1 for the null terminator
-
strcpy(arr[i], buffer;
-
and off you go.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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");
}
|
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...
|
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...
|
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...
|
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...
|
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)....
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
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,...
|
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |