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

Beginner struggling with pointer and string arrays

Hi

I'm new to C. Here's the problem. I understand the whole concept of
pointers and can easily use it with normal non-array variables.

I want to declare a array of strings, thus multidimensional character
array, in my int main();

Then I want to pass this to a function that will scanf all the values.
I also want another function to display all the names but I just can
get it working.

How do yo do it?

Thanks

Riaan
Nov 13 '05 #1
2 5525
Riaan C writes:
I'm new to C. Here's the problem. I understand the whole concept of
pointers and can easily use it with normal non-array variables.

I want to declare a array of strings, thus multidimensional character
array, in my int main();

Then I want to pass this to a function that will scanf all the values.
I also want another function to display all the names but I just can
get it working.


Fill in the blanks.

#include <stdlio.h> // exit()

void chat(char t[][20])
{
}
/*-------------------------*/
void display(char u[][20])
{
}
/*-------------------------*/
int main()
{
char s[3][20];
chat(s);
display(s);
}

Nov 13 '05 #2
ri***********@tiscali.co.uk (Riaan C) writes:
Hi

I'm new to C. Here's the problem. I understand the whole concept of
pointers and can easily use it with normal non-array variables.

I want to declare a array of strings, thus multidimensional character
array, in my int main();
No, more likely you want to declare an array of *pointers* to
strings. An array of strings is awkwards if the strings are of
different lengths, since you would still be forced to allocate
exactly the same amount of space for each string.

(Example of an array of strings:)

char aos[][6] = {
"Hello",
"there",
"bud"
};

As you can see, "bud" above still gets 6 bytes even though it
only uses 4 (counting the terminating null character). Compare
with:

(Example of an array of pointers-to-string:)

char *aopts[] = {
"Hello",
"there",
"bud"
};

The disadvantage to the above is that you must either point the
strings to string literals (as above), in which case you
shouldn't try to modify them; or you need to dynamically
(probably) allocate space for each and every element (and,
therefore, free() them also).
Then I want to pass this to a function that will scanf all the values.
I also want another function to display all the names but I just can
get it working.


I'm not sure what exactly you mean here. Please explain in more
detail what you wish to do.

-Micah
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Foxy Kav | last post by:
Hi everyone, Im currently doing first year UNI, taking a programming course in C++, for one project i have to create a simple array manipulator... that i have done, but i cant figure out how to...
5
by: Tarjei Romtveit | last post by:
I'm still a newbie into C++ programming, so I got a quite foolish string related question. Using: Dev-cpp 4.9.9.2 (I think Dev-Cpp uses a gcc compiler of some sort) If i declare a char...
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...
12
by: Joshua Rulz | last post by:
Hi, i want to learn to program im quite skilled with computers and want to learn c++. is there anyone who can teach me or tell me a good website to learn it? all replies will be appreciated.
33
by: aaron | last post by:
I have a question in my class.. hoping to get some help I need to create a program that will print firstName middleName lastName then their initials User will input: John Smith Doe Output:...
14
by: Glen Dayton | last post by:
While looking at some old code I ran across a snippet that increments a pointer to access different members of a structure: .... struct Badness { std::string m_begin; std::string m_param1;...
8
by: subramanian100in | last post by:
Supoose we have, int x; int *p = &x; int *q; q = p + 1;
8
by: isaac2004 | last post by:
hello, i posted with a topic like this but got no real feedback(prob cuz of lapse in my explanation) so i am trying it again. i am trying to set up a function that brings in a txt file and adds the...
2
by: Stang1 | last post by:
In the following code, can someone please explain to me what exactly is happenening as far as accessing a 2-dimensional array with a 1- dimensional index? What is the benifit of this? Thanks in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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: 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
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
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...

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.