LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-05-2003, 04:08 PM   #1
jetfreggel
Member
 
Registered: May 2002
Posts: 172

Rep: Reputation: 30
stdarg with varaible amout of string


i am trying to create a program that uses a variable amount of strings
and pass it to a function and return a pointer
a got it done without user input
but i want it done with user input is this possible i got this so far
but i can't get it running

Code:
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>

char *str[30];


char *func(char *str1, ...);

int main(void)
{
	char buffer[100];
	int count = 0;
	char *p;
	
	/*get the strings */
	puts("Enter the strings you want to cat, blank line exit");
	while(gets (buffer) != 0 && buffer[0] != '\0'&& count < 30)
	{
		if ((str[count] = (char *)malloc(strlen(buffer)+ 1)) == NULL)
		{
			fprintf(stderr ,"Error allocating memory");
			exit(-1);
		}
		strcpy(str[count++],buffer); 
		
	}
	str[count++] = NULL;	
	p = str[0];
	func(p);
	puts(p);
	printf("%si\n", p);
	return 0;

}	
char *func(char *str, ...)
{
	va_list ap;
	char *p ;
	
	va_start(ap, str);
	if ((p = va_arg(ap, char *)) == NULL)
	{
		*str = '\0';
		return str;
	}
	else
		strcpy(str,p);
	
	while ((p = va_arg(ap, char *)) != NULL)
		strcat(str,p);
		
	va_end(ap);
	return str;
}

but i can;t get anything on the screen
 
Old 01-06-2003, 10:11 AM   #2
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
I don't think that you have to use a variable argument list for this. Is this what you want to do: get user input and store it in a multidimentional array as an array of strings, than combine that array into one long single string with a single pointer pointing to it?
 
Old 01-06-2003, 11:59 AM   #3
jetfreggel
Member
 
Registered: May 2002
Posts: 172

Original Poster
Rep: Reputation: 30
thx for your reply

i have to use variable arguments itd part of excercice
i am making(just to learn more about stdargs.h
fisrt real program language that i am trying)
i know i could do it your way but i want to know
if will it work my way or what am i doing wrong?
 
Old 01-06-2003, 12:44 PM   #4
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
The problem is that you are using variable argument incorrectly. It is mostly used for functions that accept a variable number of arguments, and that is not the case in your example, because you are not passing it variable arguments in the call(s).

An example code using variable arguments would be the printf(format string, ...); in stdio.h

One way to accept user input of variable length is to combine malloc() and realloc(), however that is a different concept than variable arguments.

In most cases, or at least those that I am aware of, variable arguments use switch case logic. If you can, check out your compilers implementation of printf();
 
Old 01-06-2003, 02:31 PM   #5
jetfreggel
Member
 
Registered: May 2002
Posts: 172

Original Poster
Rep: Reputation: 30
thank you for pointing that out
i read back the chapter that uses variable
argument and now i got it
it ;s a bit confusing with this , pointers and then pointer to pointers

thanx again for your reply
 
Old 01-06-2003, 02:37 PM   #6
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
Yeah it is definately confusing to allocate 2d arrays, for example here is the C++ snytax for a 2d array ( an array of pointers is slightly different ... write this down somewhere! ):

Code:
int ** ppArr = 0; //declare a pointer to pointer to int for example

ppArr = new int * [10];
for ( int ix = 0; ix < 10; ++ ix)
  ppArr[ix] = new int[10];

//now you can use it as if it were defined as int ppArr[10][10];

//delete it like this
for( int ix=0; ix < 10; ++ix)
  delete [] ppArr[ix];
delete [] ppArr;
It sucks!
 
Old 01-06-2003, 02:49 PM   #7
jetfreggel
Member
 
Registered: May 2002
Posts: 172

Original Poster
Rep: Reputation: 30
thx again for the reply and the example
i have another question
could explain how graphical programming works
for example can it ne done in c
or do i have to use c++ or other another language ??

when i have finished my book i want to make my first program
a little( and i mean little) text editor
or is this an impossible task for a beginning programmer
or should i begin with something a little smaller
 
Old 01-06-2003, 03:11 PM   #8
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
Graphics programming can be done with platform specific native code or else through middleware. Those are the two basic choices. If you choose to use native code than it will most likely be C or C++, and in the case of Linux you will use XWindows toolkits.

The main toolkits are either GTK+ or else QT. If you are a C programmer than use GTK+ and if you prefer C++ than QT is better.

This is a general outlook, but technically, there are C++ extensions for GTK+ and QT can compile cross platform through emulation. When I think of cross platform compatibility though I think of virtual machine middleware like Java which has it's own windows libraries called AWT and Swing. Java is almost like an operating system in and of itself that runs ontop of most platforms (Windows, Mac, Unix).

I think that a text editor would be best for Java. I also think that most applications that an individual or small group want to write is best done in Java. I see C/C++ more as large scale software development langauges that are performed in teams of hundreds or thousands of programmers.

But to answer you question simply, you should pick up a book on GTK+ for example, Sams publishing GTK+ programming in 21 days (I glaced through this book and it appeared to be good). This will allow you to build Windows applications on Linux and perform 2d drawing. When you want to do 3d high performance graphics than you will likely use a library called OpenGL or else if you go the middleware route than Java 3d.

The Sams book on GTK+ describes how tookits built ontop of Xlib primitives. It's interesting. I wish that there were more books on Xlib and Xclient/server, but there are barely even enough books on GTK+. That's another reason why I like Java, because there is tons of documentation and support.

C -> GTK+ -> OpenGl (or MesaGL)

or

Java -> Awt & Swing => Java 3d

Last edited by GtkUser; 01-06-2003 at 03:13 PM.
 
Old 01-06-2003, 03:23 PM   #9
jetfreggel
Member
 
Registered: May 2002
Posts: 172

Original Poster
Rep: Reputation: 30
thx for the reply again
i am know learnig c from sams learning c in 21 days
and got
sams learning c++ in 21 days from a friend
i will take a look at it (at gtk++)
but i think first i am going to make the editor on my own
for the shell because i think when i going to take a look at
gtk that it will confuse me even more

my reason for learning c is to learn about the kernel and how linux works under the hood(you have to start somewhere)

isn;t it confusig working with different languages?


i know that java was developed by sun


and what do you mean by middleware


thx again for your help
 
Old 01-06-2003, 03:54 PM   #10
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
If you want to study the kernel source code than The C Programming language, or Standard C is definately the best language for that job, afterall the kernel is implemented in C, however it is also a convoluted object based kernel that should have been refactored in Standard C++ and documented in UML. Object based C is junk to read.

The computer world is confusing. It's difficult to know what language to use, however this is the basic idea.

- Understand that damn near every SYSTEM is implemented in C.
- Middleware is implemented in C++.
- SOLUTION applications are implemented in Java (or .net ... which sucks).

The biggest hurdle is understanding the difference between solutions and systems. In most cases individual programmers are interested in building solutions (whether they know it or not) and therefore middleware is superior because it allows the developer to work at a high level of abstraction through vendor specialized libraries.

A system is a low level massive application such as the operating system kernel, or a web server like Apache or a database server like Oracle, or middleware itself ( a virtual machine and a framework). These applications are hundreds of thousands or millions of lines of code.

Standard C is a light weight language, there are almost no libraries, only stdio.h, stdlib.h, etc. This is in huge contrast to middleware. In C you build your own libraries, it's a research langauge, and you generalize toward the software architectures system interface for primitive functions and toolkits or else you go even further to the hardware specification (X86 or Sparc).

Now there is a middle ground, where C and C++ are also used for solution development however it is not as smooth of a ride for lack of documentation and software architecture constraints. That is the reason why they built middleware in the first place, so if you want to build systems than you use a research language, and if you want to build solutions than use middleware. The people at Sun using C++ to build the middleware environment that hosts soltuion development, in other words, Java was not suddenly there, it was built by about 3,000 people who built it in C++ and continue to add specialized libraries to the framework.

One of the other assets of middleware is that the Java middleware runs cross platform, so if you write you text editor it will run on Win9X, Win2k/XP, Linux, Solaris, as well as operating systems that have not been released yet. On the other hand .net middleware sucks because Microsoft purposely didn't make the platform run on different software architectures. The reason is because they are a monopoly and they don't want anyone else to use another operating system.

Learning C is one thing, and learning an object based design using C is quite another thing. It's kind of like sending a midget to fuck Ashley Judd. It doesn't do her justice. That's what you might find in the Linux kernel or the Microsoft Windows system interfaces.

I think that when you build an application than it should be modeled in a modeling language like the UML ( www.omg.org ) and implemented with an iterative and incremental process. At least if the code is over 10, 000 lines.

Java is made by Sun but the SDK is free and it runs on Linux < http://members.shaw.ca/trollking/ >.

I've built a small database using C, if you want, I'd help you with something like that, but I've never tried a text editor. I'd probably rather not write a console text editor, but rather one that runs in a GTK or Java window (and uses threads in Java).

You might want to learn about linked lists. One cool thing about C/C++/Java is that they all have similarities in terms of syntax so whatever road you start out on, you can carry on some of the knowledge.
 
Old 01-06-2003, 04:20 PM   #11
jetfreggel
Member
 
Registered: May 2002
Posts: 172

Original Poster
Rep: Reputation: 30
thx for the large reply

you made it now clear to me the difference between
c/c++ and java(sort of multi os programming language lower then a scripting language and a lower like c/c++/assembler(i know
differnet class from c lowerlower level))
the difference however between c and c++ not yet
but i think it will become clear after you inform or i the other c++
sams book)

however after looking in google who ashley judd was
(i don't no she seems the for a good old fashion midget....)

i have learned i think the confussing basics of a single linked list

and yes i like to accept that offer of building of some sort
of dbase in c cause i don't know any other laguage at the moment


modeling language like uml i read a bit of the site but this makes it more cinfussing then ever

Last edited by jetfreggel; 01-06-2003 at 04:44 PM.
 
Old 01-06-2003, 05:06 PM   #12
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
Java is a programming language, but it's better to think of Java simply as middleware. The java language does not exist independantly of the framework and virtual machine. Java is a system.

On the other hand C/C++ is a low level, light weight, research and development langauge used primarily for system implementation of large scale software.

The Standard C++ programming language is a specialization of the 1985 version of the C programming language.

The main differences between C and C++ is attributed to design features. The C langauge supports primarily action based design while C++ design is primarily object oriented. Another way to say it is that C is a procedural language and C++ is an object oriented langauge.

The honest truth is not as simple because Standard C++ supports multi-paradigms which include, procedural (action based), object based, object oriented, and genaric paradigms. On the other hand Standard C supports procedural and object based paradigms.

If you want to write a database that runs as a console application than email me at: trollking@shaw.ca

Give me an idea of what kind of database you want to make. I once constructed a movie database.

Databases probably should be generic, however that requires much more work. One cool thing would be to go ahead and write a specific console database in C than port it to a windows type application or even Java. That's how UML works, you document the application in a modeling language and than you can reuse the logic.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to limit the amout of disk space for Samba user fieldyweb Linux - Newbie 1 10-03-2005 09:30 AM
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM
What the heck... Problems with stdarg? Cannot put out float figures. Zacharias Programming 3 08-07-2004 09:03 PM
error in assigning the iterator to refrence varaible ashwinipahuja Programming 1 06-18-2004 05:55 AM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration