Hello, could anyone help me with C language a little. I'm trying to write a program with pointers, which would scan some words to the array of strings and would print them on a screen. I'm new to C programming, that's why it's so difficult to deal with such a simple task -
#include <stdio.h>
-
#include <string.h>
-
-
int main ()
-
{
-
char* string[3];
-
int j;
-
for(j=0;j<3;j++)
-
{
-
-
fgets(string[j], sizeof string[j], stdin);
-
-
}
-
-
for(j=0;j<3;j++)
-
{
-
printf("String number %d: \"%s\"\n", j, string[j]);
-
}
-
return 0;
-
}
-
3 1010 Banfa 9,065
Expert Mod 8TB
You problem is your declaration of string. You declare it as an array of 3 pointers but you never point them at valid data.
In a real life application you may want to allocate the data for those pointers from the heap, however for now I believe you program would be improved by changing the declaration of string to
That is an array of array of chars.
The basic problem here is that you want to enter three strings but all you have defined is an array of three pointers. Those pointers have to point to memory that holds the string.
Usually, you have a fixed buffer, say 80, and you fgets into that buffer. Then you can strlen what's in the buffer, malloc the correct memory, copy the buffer to string[j] and then go after the next string.
Later, you will learn techniques that don't require a fixed 80 for the program to work but, for now, just use a buffer of known size and be sure to keep your data entry within that buffer size.
Thanks a lot, I did it :))) I have never realized that *string[3] is not equal to array string[3][80], and that the first is an array of pointers, and the second is an array of chars, because the type of both is the same (char). thanks very much :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Krzysztof |
last post by:
Hi!
I have a class:
some_class{
private:
char* seq;
public:
some_class(char* se);
char* ret_seq();
....
|
by: Sh0t |
last post by:
Everytime i compile this program the pointer seems to lose its data??
*fileToFix is suppose to change after entering fixFile() as a param
program ------------------------------------------------...
|
by: Davy |
last post by:
Hi all,
I found
char x={"my"};
can be compiled.
But
char x;
x={"my"};
can not be compiled.
|
by: gaga |
last post by:
I can't seem to get this to work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *names;
char **np;
|
by: ketema |
last post by:
Hello, I was wondering if someone could help me with a function I am
trying to write. The purpose of the function is to read in text from a
file in the following format:
FIRSTNAME LASTNAME...
|
by: Carramba |
last post by:
hi!
the code is cinpiling with gcc -ansi -pedantic. so Iam back to my question
Iam trying to make program were I enter string and serach char.
and funktion prints out witch position char is...
|
by: ajikoe |
last post by:
Hi,
I tried to follow the example in swig homepage.
I found error which I don't understand.
I use bcc32, I already include directory where my python.h exist in
bcc32.cfg.
/* File : example.c...
|
by: Aris |
last post by:
A few days ago I asked from you how to join a string like "file"
A number that change values from 1 to 5 and another string like ".txt"
to have a result like "file1.txt","file2.txt" and so on
...
|
by: Bob |
last post by:
I have been working on the following program. The goal is to have a
tokenizing routine that avoids some of the problems of strtok(), the
comments should explain the features.
This runs fine on...
|
by: sam_cit |
last post by:
Hi Everyone,
I have the following structure in my program
struct sample
{
char *string;
int string_len;
};
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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,...
| |