473,395 Members | 1,762 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,395 software developers and data experts.

Problem with pointers and string array. C.

6
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
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main ()
  5. {
  6.     char* string[3];
  7.     int j;
  8.     for(j=0;j<3;j++)
  9.     {
  10.  
  11.     fgets(string[j], sizeof string[j], stdin);
  12.  
  13.     }
  14.  
  15.     for(j=0;j<3;j++)
  16.     {
  17.         printf("String number %d: \"%s\"\n", j, string[j]);
  18.     }
  19.     return 0;
  20. }
  21.  
Mar 18 '08 #1
3 1010
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1.     char* string[3];
  2.  
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

Expand|Select|Wrap|Line Numbers
  1.     char string[3][80];
  2.  
That is an array of array of chars.
Mar 18 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
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.
Mar 18 '08 #3
Cheer
6
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 :)
Mar 18 '08 #4

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

Similar topics

4
by: Krzysztof | last post by:
Hi! I have a class: some_class{ private: char* seq; public: some_class(char* se); char* ret_seq(); ....
1
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 ------------------------------------------------...
28
by: Davy | last post by:
Hi all, I found char x={"my"}; can be compiled. But char x; x={"my"}; can not be compiled.
19
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;
1
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...
3
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...
2
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...
10
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 ...
13
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...
22
by: sam_cit | last post by:
Hi Everyone, I have the following structure in my program struct sample { char *string; int string_len; };
0
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...
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
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
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
jinu1996
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...
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,...

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.