Sponsored Content
Top Forums Programming Scanf() string pointer problem Post 302874187 by yifangt on Thursday 14th of November 2013 11:39:02 AM
Old 11-14-2013
malloc to scanf() string

Thanks Corona688!

I had thought Line 21 covers the allocation of studentName.
Code:
list[x].studentName=malloc(20);

I tried putting this line after Line 25, compiled fine, but did not work. Segment fault! again.
I am trying to use pointer. Where should I put this line for the correct way, not only to make the program run?
Thanks a lot again.
 

10 More Discussions You Might Find Interesting

1. Programming

Scanf problem under LINUX...

I have a problem reading characters from keyboard with the scanf function. Here there is a little piece of code: #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> /* The last 3 libraries are included because in the real program I use some... (4 Replies)
Discussion started by: robotronic
4 Replies

2. Programming

Problem with function which reutrns pointer to a value

i have a function: char *pcCityIdToCountryName(ADMIN_DB_DATA *pstHEader, unit uiCityID) this returns a pointer to CountryName if cityId is given. to retrieve countryname i give: char *CountryName; CountryName = pcCityIdToCountryName(..................); but when i compile it is giving :... (5 Replies)
Discussion started by: jazz
5 Replies

3. Programming

problem with scanf

hi all! i've written a simple c program: #include<stdio.h> #include<stdlib.h> int main() { int a; char b; char c; ... (4 Replies)
Discussion started by: mridula
4 Replies

4. Programming

pointer problem

could any one tell why the following is showing segmentation fault while using **ptr but working fine using **a #include<stdio.h> ... (1 Reply)
Discussion started by: useless79
1 Replies

5. Programming

String and pointer problem

i am having a string like " X1 " ---> string lenght is 30 I have stored this to a chararry . ref so here ref = " X1 " now i trim the left space by my function . Si the string now becomes "X1 " ---> string lenght is 15... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

6. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

7. Programming

segfault in pointer to string program

hello all, my question is not about How code can be rewritten, i just wanna know even though i am not using read only memory of C (i have declared str) why this function gives me segfault :wall:and the other code executes comfortably though both code uses same pointer arithmetic. ... (4 Replies)
Discussion started by: zius_oram
4 Replies

8. Programming

pointer problem

Does anyone know? int x = 1; int *p = &++x; //ok ! int *q = &x++; //gives an error :O why the first pointer is ok but the second is an error? (13 Replies)
Discussion started by: nishrestha
13 Replies

9. Programming

How i use pointer as a string in c programing?

I'm newbie learner. My all friend use windows just only me use linux. so i can't solve any problem by myself. i need a solution. how can i use pointer as a string. #include<string.h> #include<stdio.h> int main() { char *s='\0'; gets(s); puts(s); return 0; } This code work on... (6 Replies)
Discussion started by: raihan004
6 Replies

10. Programming

String pointer does not work

Hello, I am trying to reverse complement one string and reverse another (NO complement!), both with pointer. My code compiled without error, but did not do the job I wanted. #include <stdio.h> #include <stdlib.h> #include <zlib.h> #include "kseq.h" // STEP 1: declare the type of file... (5 Replies)
Discussion started by: yifangt
5 Replies
SCANF(3)						     Library Functions Manual							  SCANF(3)

NAME
scanf, fscanf, sscanf, vscanf, vfscanf, vsscanf - formatted input conversion SYNOPSIS
#include <stdio.h> #include <stdarg.h> int scanf(const char *format [, pointer] ...) int fscanf(FILE *stream, const char *format [, pointer] ...) int sscanf(const char *s, const char *format [, pointer] ...) int vscanf(const char *format, va_list args) int vfscanf(FILE *stream, const char *format, va_list args) int vsscanf(const char *s, const char *format, va_list args) DESCRIPTION
Scanf reads from the standard input stream stdin. Fscanf reads from the named input stream. Sscanf reads from the character string s. Each function reads characters, interprets them according to a format, and stores the results in its arguments. Each expects as arguments a control string format, described below, and a set of pointer arguments indicating where the converted input should be stored. The v*scanf functions can be used to make functions like the first three by using the stdarg(3) method to process the argument pointers. The control string usually contains conversion specifications, which are used to direct interpretation of input sequences. The control string may contain: 1. Blanks, tabs or newlines, which match optional white space in the input. 2. An ordinary character (not %) which must match the next character of the input stream. 3. Conversion specifications, consisting of the character %, an optional assignment suppressing character *, an optional numerical maximum field width, and a conversion character. A conversion specification directs the conversion of the next input field; the result is placed in the variable pointed to by the corre- sponding argument, unless assignment suppression was indicated by *. An input field is defined as a string of non-space characters; it extends to the next inappropriate character or until the field width, if specified, is exhausted. The conversion character indicates the interpretation of the input field; the corresponding pointer argument must usually be of a restricted type. The following conversion characters are legal: % a single `%' is expected in the input at this point; no assignment is done. d a decimal integer is expected; the corresponding argument should be an integer pointer. o an octal integer is expected; the corresponding argument should be a integer pointer. x a hexadecimal integer is expected; the corresponding argument should be an integer pointer. s a character string is expected; the corresponding argument should be a character pointer pointing to an array of characters large enough to accept the string and a terminating `', which will be added. The input field is terminated by a space character or a new- line. c a character is expected; the corresponding argument should be a character pointer. The normal skip over space characters is suppressed in this case; to read the next non-space character, try `%1s'. If a field width is given, the corresponding argument should refer to a character array, and the indicated number of characters is read. efg a floating point number is expected; the next field is converted accordingly and stored through the corresponding argument, which should be a pointer to a float. The input format for floating point numbers is an optionally signed string of digits possibly contain- ing a decimal point, followed by an optional exponent field consisting of an E or e followed by an optionally signed integer. [ indicates a string not to be delimited by space characters. The left bracket is followed by a set of characters and a right bracket; the characters between the brackets define a set of characters making up the string. If the first character is not circumflex (^), the input field is all characters until the first character not in the set between the brackets; if the first character after the left bracket is ^, the input field is all characters until the first character which is in the remaining set of characters between the brackets. The corresponding argument must point to a character array. The conversion characters d, o and x may be capitalized or preceded by l to indicate that a pointer to long rather than to int is in the argument list. Similarly, the conversion characters e, f or g may be capitalized or preceded by l to indicate a pointer to double rather than to float. The conversion characters d, o and x may be preceded by h to indicate a pointer to short rather than to int. The scanf functions return the number of successfully matched and assigned input items. This can be used to decide how many input items were found. The constant EOF is returned upon end of input; note that this is different from 0, which means that no conversion was done; if conversion was intended, it was frustrated by an inappropriate character in the input. For example, the call int i; float x; char name[50]; scanf("%d%f%s", &i, &x, name); with the input line 25 54.32E-1 thompson will assign to i the value 25, x the value 5.432, and name will contain `thompson' . Or, int i; float x; char name[50]; scanf("%2d%f%*d%[1234567890]", &i, &x, name); with input 56789 0123 56a72 will assign 56 to i, 789.0 to x, skip `0123', and place the string `56' in name. The next call to getchar will return `a'. SEE ALSO
atof(3), getc(3), printf(3), stdarg(3). DIAGNOSTICS
The scanf functions return EOF on end of input, and a short count for missing or illegal data items. BUGS
The success of literal matches and suppressed assignments is not directly determinable. 7th Edition May 15, 1985 SCANF(3)
All times are GMT -4. The time now is 09:41 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy