?
Solved

how can i store data in to a array

Posted on 2010-11-30
8
Medium Priority
?
330 Views
Last Modified: 2012-05-10
hallo,
im trying to put data in to array its a name

but i dont know how

please help.
#include <stdio.h>
#include <conio.h>
int main (void)
{
 char vnaam[50];
printf("Wat is u voornam ?");
scanf("%s",vnaam[50]);
getchar();
printf("well look %s",vnaam[50]);
getchar();    
}

Open in new window

0
Comment
Question by:kensy11
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34242336
You don't need to specify the array index when writing to it:
#include <stdio.h>
#include <conio.h>
int main (void)
{
 char vnaam[50];
printf("Wat is u voornam ?");
scanf("%s",vnaam);
getchar();
printf("well look %s",vnaam);
getchar();    
}

Open in new window

0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 34242377
change the following statement:
scanf("%s",vnaam[50])
with
scanf("%s",vnaam)
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 1000 total points
ID: 34242470
fyi - vnaam[50] in the scanf is simply the 51st entry into the vnaam array.
But since you define vnaam[50] to have only 50 entries, then you are actually referencing a char that is outside of the array. In some instances, your program will crash with a segment fault if you do this.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:kensy11
ID: 34242774
thanks my code looks like this

i have another question what if i dont know how long the array will be, now its 50 but can just leave it blank ?



#include <stdio.h>
#include <conio.h>
int main (void)
{
 char vnaam[];
printf("Wat is u voornam ?");
scanf("%s",vnaam);
getchar();
printf("well look %s",vnaam);
getchar();    
}

Open in new window

0
 
LVL 32

Expert Comment

by:phoffric
ID: 34242949
>> char vnaam[];
Sorry, this is not valid. If you are filling up an array with scanf, you should make it much larger than any possible entry. After the entry, it will be null terminated.
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 500 total points
ID: 34242964
I don't think you can. Normally you would size the char[] to the maximum size you were expecting.

#define MAX_STRING_SIZE 80

int main()
{
    char[MAX_STRING_SIZE] buffer;

    // scanf, etc, etc
}
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 1000 total points
ID: 34243010
To protect from overflow, you should consider using fgets which limits the number of chars transferred to the buffer. (In this case, from the console, you would use stdin as the file handle.)
   http://www.cplusplus.com/reference/clibrary/cstdio/fgets/

But read this carefully w.r.t. newline char and null termination.
0
 
LVL 17

Expert Comment

by:CSecurity
ID: 34250522
I'm not sure about scanf function but I remember that cin function in C++ takes 512 characters, not more.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question