473,473 Members | 1,484 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

printing of array of strings is printing null instead of printing the result

1 New Member
i m trying to print the array of string using this code but the compiler is printing null in the output:the code is as follows:
Expand|Select|Wrap|Line Numbers
  1. char *arr[10];
  2. int i;
  3. for(i=0;i<10;i++)
  4. scanf("%s",*(arr+i));
  5.  
  6. for(i=0;i<10;i++)
  7. printf("%s\n",*(arr+i));
Feb 17 '14 #1
3 1222
stdq
94 New Member
Hello! When you're reading each character to the array, use %c instead of %s. Also, when you're printing each character, use %c, or, more simply, just use

Expand|Select|Wrap|Line Numbers
  1. printf("%s", arr)
instead of trying to print each character separately. If you prefer to do it that way, use %c instead of %s, and print only until you find the terminating '\0' character.

There is one more thing: you should either declare the array with 11 positions or only read until i is 8, because there must be space for the '\0' character, which, in this program, I'm not sure would be inserted automatically.
Feb 17 '14 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
@stdq
This is about an array of strings and not a single string.

@abhay123
Your array is an array of 10 pointers to char. I'm guessing you want each pointer to point to a string. However, you have not allocated memory for any strings.

You cannot scanf into arr using %s because %s is for a string. A string is an array of char whereas your array is an array of char pointers.
Feb 18 '14 #3
stdq
94 New Member
@weaknessforcats: sorry about that! ^^
Feb 18 '14 #4

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

Similar topics

12
by: James | last post by:
Hi, Have posted before, but will simplify problem here. For original post go to http://forums.devshed.com/t80025/s.html I have setup 2 arrays like so in my one page script: $carers =...
3
by: Dinçer | last post by:
Hello, I am trying to call a simple web service from .net environment. The service gets an array parameter. When I run it, it becomes the error: Server was unable to process request. --> Array...
0
by: Dinçer | last post by:
Hello, I am trying to call a simple web service from .net environment. The service gets an array parameter. When I run it, it becomes the error: Server was unable to process request. --> Array...
4
by: _Mario.lat | last post by:
Hallo, I have a little question: In the function session_set_save_handler I can pass the name of function which deal with session. In Xoops code I see the use of this function like that: ...
29
by: Ron Garret | last post by:
>>> u'\xbd' u'\xbd' >>> print _ Traceback (most recent call last): File "<stdin>", line 1, in ? UnicodeEncodeError: 'ascii' codec can't encode character u'\xbd' in position 0: ordinal not in...
4
by: Charles | last post by:
I'm trying an example from the book "Learning PHP5" but it doesn't work as Expected. Snippet: <form method="POST" action="index.php"> <select name="lunch" multiple> <option value="pork">BBQ...
3
by: 7stud | last post by:
Can anyone tell me why I can print out the individual variables in the following code, but when I print them out combined into a single string, I get an error? symbol = u'ibm' price = u'4 \xbd'...
2
by: nagesh0280 | last post by:
Hi experts, I'm from a Verilog HDL background and trying to learn C. There are a lot of similarities between Verilog and C but the concept of char arrays and strings has me confused. I'd...
18
by: sanjay | last post by:
Hi, I have a doubt about passing values to a function accepting string. ====================================== #include <iostream> using namespace std; int main() {
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
1
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...
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...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.