Sponsored Content
Top Forums Programming C; storing strings in an array Post 302643861 by neutronscott on Sunday 20th of May 2012 10:32:44 PM
Old 05-20-2012
you merely set it to point to that same address each time. C has char type. a string is pointer (char *), so the variable itself is only memory location of the start. the end is when you hit a \0. so when you do line = malloc() you grab enough space for 79 chars and the null. getline writes to it, but it doesn't change the address.. that address stays the same here.

setting a pointer to a pointer only copies the address. you need to copy what's at the address. this requires a loop, continuing either a set length like with memcpy, or until null byte, strcpy.

pointers are sometimes a tough concept to fully grasp

edit: yes, the +1 to the malloc would be so you have room to store a \0 which is needed to terminate strings.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Storing pointer array in C

All .. I am having a pointer array . And trying to store the addess into that pointer array . please see below the problem i faced code: int cnt1; char *t_array; char *f_array; for(cnt1=0; cnt1<1000; cnt1++) { t_array =... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies

2. Shell Programming and Scripting

storing variables in array.Please help

Hi All, I need some help with arrays. I need to take input from the user for hostname, username and password until he enters .(dot) or any other character and store the values in the variable array. I would further connect to the hostname using username and passwd and copy files from server to... (7 Replies)
Discussion started by: nua7
7 Replies

3. Shell Programming and Scripting

storing records in awk array

hi i have a file as follows: 1 2 3 4 5 6 i want to store all these numbers in an array using awk.. so far i have: awk '{for(i=1;i<=NR;i++) {a=$1}} END {for(i=1;i<=NR;i++) {printf("%1.11f",a)}}' 1.csv > test however, i am getting all values as zero in the "test" file..... (3 Replies)
Discussion started by: npatwardhan
3 Replies

4. Shell Programming and Scripting

storing values in a list or array

i have a file called file.txt having the following entries. 2321 2311 2313 4213 i wnat to store these values in a list and i want to iterate the list using loop and store it in another list (1 Reply)
Discussion started by: KiranKumarKarre
1 Replies

5. Shell Programming and Scripting

Storing blanks in an array in bash

Hi Guys, I have a file which is as follows: 1 2 4 6 7 I am trying to store these values in an array in bash. I have the following script: FILE=try.txt ARRAY=(`awk '{print}' $FILE`) echo ${ARRAY} (3 Replies)
Discussion started by: npatwardhan
3 Replies

6. UNIX for Dummies Questions & Answers

storing strings in C

i'm trying to figure out how to store strings into an array, can someone help me figure it out? i would like to store the following for example: array = "dog" array = "cat" the inputs are coming from the command line. Thanks, (6 Replies)
Discussion started by: l flipboi l
6 Replies

7. Shell Programming and Scripting

Storing data in perl 2D array

Respected All, Kindly help me out. I have got file listings in a directory like this: -rw-r--r-- 1 root root 115149 2011-11-17 07:15 file1.stat.log -rw-r--r-- 1 root root 115149 2011-11-18 08:15 file2.stat.log -rw-r--r-- 1 root root 115149 2011-11-19 09:15 file3.stat.log -rw-r--r-- 1... (2 Replies)
Discussion started by: teknokid1
2 Replies

8. Shell Programming and Scripting

Storing command output in an array

Hi, I want keep/save one command's output in an array and later want to iterate over the array one by one for some processing. instead of doing like below- for str in `cat /etc/passwd | awk -F: '$3 >100 {print $1}' | uniq` want to store- my_array = `cat /etc/passwd | awk -F: '$3 >100 {print... (4 Replies)
Discussion started by: sanzee007
4 Replies

9. Shell Programming and Scripting

Storing two dimensional array for postprocessing

Hi Community, Would love to get some quick help on below requirement. I am trying to process mpstat output from multiple blades of my server I would like to assign this the output to an array and then use it for post processing. How can I use a two dimensional array and assign these value ... (23 Replies)
Discussion started by: sshark
23 Replies

10. UNIX for Beginners Questions & Answers

Storing timestamp of files in an array

Hi, I have written the below script to get the timestamp of each files and result is as below Script find /home/user -type f -name "*.json" -printf '%Tc %p\n' | awk {'print $1 " " $2 " " $3 " " $4 " " $5 " " $6 " " $7'} Input -rw-r--r-- 1 user domain users 17382 Jul 19 06:10... (5 Replies)
Discussion started by: nextStep
5 Replies
wcstombs(3)						     Library Functions Manual						       wcstombs(3)

NAME
wcstombs, wcsrtombs - Converts a wide-character string into a multibyte-character string LIBRARY
Standard C Library (libc.so, libc.a) SYNOPSIS
#include <stdlib.h> size_t wcstombs( char *s, const wchar_t *pwcs, size_t n); #include <wchar.h> size_t wcsrtombs( char *s, const wchar_t **pwcs, size_t n, mbstate_t *ps ); STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: wcstombs(): ISO C, XPG4 wcsrtombs(): ISO C Refer to the standards(5) reference page for more information about industry standards and associated tags. PARAMETERS
Points to the location where the converted multibyte-character string is stored. Points or indirectly points to the address of the wide- character string to be converted. Specifies the maximum number of bytes to be stored in the output variable (s). Points to an mbstate_t structure containing the conversion state of the data in pwcs. DESCRIPTION
The wcstombs() function converts a wide-character string into a multibyte-character string and stores the result in a location pointed to by the s parameter. The wcstombs() function stores only the number of bytes specified by the n parameter as the output string. When copy- ing between objects that overlap, the behavior of wcstombs() is undefined. The behavior of the wcstombs() function is affected by the LC_CTYPE category of the current locale. In environments that use shift-state- dependent encoding, the array pointed to by s begins in its initial shift state. The wcstombs() function converts each character in the pwcs wide-character string and stores the converted character in the s string. The wcstombs() function stops storing characters in the output array under the following conditions: The function has encountered a null wide character in the pwcs wide-character string and has written a corresponding null byte in s. The function cannot store the next converted wide character because there is not enough room in s; that is, the function would have to store more than n bytes in the s parameter to store the character. The function has already stored n bytes in the s parameter. The function has encountered an invalid wide-character code in the pwcs wide-character string. If the wcstombs() function stores exactly n bytes in the s parameter, the function does not store a terminating null byte. To ensure that there is enough room in the s parameter to fit the converted string, the application should allocate enough memory to store the maximum multibyte-character string based on the number of wide characters in the wide-character string. Allocate the number of bytes (and pass the value in the n parameter) equal to the length of the pwcs wide-character string (as determined by the wcslen() or wcsrlen() function) multiplied by the value of MB_CUR_MAX in the current locale. A prior call to wcstombs() or wcsrtombs() can perform this calcula- tion for the application. For example, on the first call to wcstombs() or wcsrtombs(), the application can pass s as a null pointer to obtain the number of bytes (not including the terminating null byte) required for s. The application then uses this return value as the value for n in the call to wcstombs() or wcsrtombs() that performs the conversion. The wcsrtombs() function is a restartable version of wcstombs(). Restartable conversion functions obtain and store the conversion state in an mbstate_t structure that can be read and changed by subsequent calls to the same or other restartable conversion functions. Results are undefined when an application uses restartable and nonrestartable conversion functions with the same source and destination variables. For example, an application would use wcsrlen() rather than wcslen() to determine the length of pwcs if that variable were to be processed with wcsrtombs() rather than wcstombs(). RESTRICTIONS
The wcsrtombs() and other restartable versions of conversion routines are functional only when used with locales that support shift state encoding. Currently, the Tru64 UNIX product does not provide any locales that use shift state encoding, so the wcstombs() and wcsrtombs() functions do not differ in terms of run-time behavior. RETURN VALUES
If the wcstombs() and wcsrtombs() functions do not encounter an invalid wide-character code, they return the number of bytes stored, not including the terminating null byte. When these functions encounter a wide-character code that does not correspond to a valid multibyte character, they return a value of -1 cast to size_t and set errno to indicate the error. If the wcstombs() and wcsrtombs() cannot store all of the converted characters in the output array, the functions stop before storing a character that would overflow the output array and return the number of bytes stored in the array. When the return value is n, the output array is not null terminated. If these functions are called with a null pointer value for s, they return the number of bytes required for the output character array. ERRORS
If any of the following conditions occur, the wcstombs() and wcsrtombs() functions set errno to the corresponding value: The array pointed to by the pwcs parameter contains an entry that does not correspond to a valid multibyte character. RELATED INFORMATION
Functions: btowc(3), mblen(3), mbsinit(3), mbstowcs(3), mbtowc(3), wcslen(3), wctob(3), wctomb(3) delim off wcstombs(3)
All times are GMT -4. The time now is 06:30 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy