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

How to define string of unknown length

3
how to define a string of undefined length in c programming???
Mar 7 '14 #1
1 13969
Nepomuk
3,112 Expert 2GB
It isn't a silly question by any means - C is very difficult in terms of string processing. As you probably know, a string in C is an array of chars followed by a binary 0 (which is written '\0'). And arrays are by their very nature defined to have a certain length.

I know of no really easy way to address this problem but there are a few tricks that can be used. Depending on the exact problem you're facing, you could:
  • Just define a big array which can take all possible strings you may want to put there
  • Use realloc to change the size of the array if you need more space
  • Use a pointer to a string and then change which string that pointer points to
There are other solutions you could also consider - for example, if you really want true independence from the array structure you could define your own String type as a linked list (though you wouldn't be able to use the predefined string functions with that of course). But as I said, I know of no really elegant solution.
Mar 7 '14 #2

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

Similar topics

8
by: M. Clift | last post by:
Hi All, I have a list of varying length. Would someone know the way to get the last two values for this? I can see how this is done with a list that I know the length of, but not one thats...
5
by: wolfgang haefelinger | last post by:
Greetings, I'm trying to read (japanese) chars from a file. While doing so I encounter that a char with length 2 is returned. Is this to be expected or is there something wrong? Basically...
1
by: Wee | last post by:
String.value.length and String.value doesn't work together in a single function in IE. I've no problem running either of this, but not together at the same time. var...
97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
2
by: Pierre | last post by:
I need to check the length of a string in a textbox control. I used RegularExpressionValidator with ".{0,20}" to check if the string is between 0 and 20 characters. Is it possible to use the other...
11
by: TomServo | last post by:
I am writing code that needs to run on a variety of Unix systems. I am calling the statvfs and statfs system calls and I need to to convert some of the integers returned to character strings....
4
by: Anthra Norell | last post by:
Hi, I keep working around a little problem with unpacking in cases in which I don't know how many elements I get. Consider this: def tabulate_lists (*arbitray_number_of_lists): table = zip...
1
by: SoFaraway | last post by:
Hi all, In C, to read a line from a file, we need to allocate some fixed length of memory first. However, if the line is longer than the length of the allocated memory, it can't be read correctly....
23
by: Himanshu Chauhan | last post by:
Hi! I was wondering, In the first parse of a singly linked list of unknown length, is it possible to know when we are at middle of the linked list? Regards --Himanshu
0
by: jonathan184 | last post by:
Hi I am pretty new to .net I tried to make a console client to call wcf service and print the response in the console. I get this error when i run the code. Error in deserializing body of reply...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.