Obviously declared variable is considered undeclared [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have the following code* inside an if else {} code block but the GCC compiler throws me this error:
hw2b.c: In function `printSymbolK':
hw2b.c:26: parse error before `int'
hw2b.c:27: `w' undeclared (first use in this function)
hw2b.c:27: (Each undeclared identifier is reported only once
hw2b.c:27: for each function it appears in.)
*
int w = 0;
for(w = 4; w < i; w++) {
printf(" ");
}
Can someone tell me what i'm missing here? The w variable is declared and initialized inside the if else block but it says it isn't. Any ideas?
EDIT: You were right, i only copied a small piece of my program. Here is the full code:
#include <stdio.h>
/*
* The printSymbolK function accepts a
* natural number (N > 3) and prints the
* symbol K to the console according to certain
* specifications.
*/
void printSymbolK(int N) {
int i = 0,k=0;
int katoAkeraioMeros = N/2; // to apotelesma tou pilikou tha strogkilopoihtei pros ta kato dinontas mas to kato akeraio meros tou N/2
char star = '*';
for(i = 0;i < N; i++) {
if( i < katoAkeraioMeros) { // katoAkeraioMeros - i+1 = posa tha einai apo pano
int res = katoAkeraioMeros - 1; // poses fores tha trexei i epanalipsi
int l = 0;
printf("%c",star);
for(l = i;l < res;l++) {
printf(" ");
}
printf("%c\n",star);
} else if(i+1 == katoAkeraioMeros) {
printf("%c%c\n",star,star);
} else if(i > katoAkeraioMeros) { // auxise to space kata 1 gia kathe i apo edo kai pera
printf("%c",star);
int w = 0;
for(w = 4; w < i; w++) {
printf(" ");
}
printf("%c\n",star);
} else {
printf("%c %c\n",star,star);
}
}
}
int main() {
// Stelios Papamichail 4020
int n = 0;
do {
scanf("%d",&n);
printf("\n\n");
} while(n <= 3);
printSymbolK(n);
return 0;
}

the first error says it all:
hw2b.c:26: parse error before `int'
So something triggered a syntax error before the int token.
you shouldn't worry about further errors. at this point the parser is confused and thinks you're using w whereas you're trying to declare it (compilers don't stop at the first error so you can fix several errors at once, but sometimes that backfires, and sometimes they stop at some error, and when you fix it, you get more errors... compiler does its best with invalid code)
Just fix the first error, and retry.

Without seeing the full code, I would guess your using an older C compiler (C89) and you have code that is neither variable definition nor declarations above 'int w = 0;'.

Related

My variables aren't working, keep recieving 'expression result unused' error

I'm currently working on a project which requires me to make a right aligned triangle of # marks. I'm receiving an error from my variable 'space'. Could someone tell me why I am receiving this error?
#include <cs50.h>
#include <stdio.h>
int get_height(void);
int lineno;
int column_fill;
int main(void)
{
int height = get_height();
int space = height;
for (lineno = 1; lineno <= height; lineno++ )
{
for (space; space > 0; space--)
{
printf(".");
}
for (column_fill = 1; column_fill <= lineno; column_fill++)
{
printf("#");
}
printf("\n");
}
}
int get_height(void)
{
int height;
do
{
height = get_int("Height: ");
}while(height < 0 || height > 9);
return height;
}
The error I'm recieving is:
mario.c:13:14: error: expression result unused [-Werror,-Wunused-value] for (space; space > 0; space--) ^~~~~ 1 error generated. : recipe for target 'mario' failed make: *** [mario] Error 1
In for (space; space > 0; space--), the first space does not do anything, and the compiler is warning you about this.
Commonly, the first item in a for statement is either an expression that does something, such as an assignment space = height, or a declaration of one or more objects to be used in the loop, such as int space = height. Change your code to one of these, and the compiler will stop complaining.
You likely should use the latter and remove the earlier separate declaration of space because:
You need to reset space each time this loop starts.
It is preferable to keep declarations as local as possible, to avoid opportunities for mistakes.

What can caused run time error in C besides array for uVA 11192 Group Reverse?

I don't know what caused my code to get run time error because the array needed for uVA 11192 is only [101] (minimum) and I don't use any pointer here.
int div = 1;
char arr[110];
while(div>0)
{
scanf("%d",&div);
getchar();
scanf("%s",arr);
getchar();
len=strlen(arr);
length=len/div;
for(int i=1;i<=div;i++)
{
right = (length*i)-1;
for(int j=length;j>=1;j--)
{
printf("%s",arr[j]);
arr[j] = arr[102];
}
left = length*i;
puts("");
}
left=0;
}

C Segmentation fault function reference [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
The if(ptr->is_word) on the sizeH(node *ptr) function is causing a segmentation fault.
dictionary.c:120:13: runtime error: member access within null pointer of type 'node' (aka 'struct node')
dictionary.c:120:13: runtime error: load of null pointer of type '_Bool'
Segmentation fault
If I add an if condition to return a specific value before this if-condition, the code executes and I make sure the root isn't null
so I guess the problem is the ptr isn't passed correctly to the sizeH function.
unsigned int size(void)
{
node *ptr = root;
printf(ptr->is_word ? "true" : "false");
unsigned int count = sizeH(ptr);
printf("COUNTER IN SIZE %d\n" , count);
return 0;
}
unsigned int sizeH(node *ptr){
if(ptr->is_word)
{
return 1;
}
else
{
for(int x = 0; x < N; x++){
return 0 + sizeH(ptr->children[x]);
}
}
return 0;
}
There's a couple of fundamental problems with your sizeH function. Firstly it doesn't check that the ptr that is passed in is not NULL. This is very likely to happen as your function is recursive and you have this loop that is called for each child node.
for(int x = 0; x < N; x++){
return 0 + sizeH(ptr->children[x]);
}
Except that the code in the loop is wrong and it'll only ever be called for the first child, which is the second issue. Because you use return inside the loop, it'll only ever run once. Instead you should tally up the values returned for each child node and then return that.
Making the two changes above would leave your function looking like this.
unsigned int sizeH(node *ptr) {
if(ptr==NULL) {
return 0;
} else if(ptr->is_word) {
return 1;
} else {
unsigned int ret = 0;
for(int x = 0; x < N; x++) {
ret += sizeH(ptr->children[x]);
}
return ret;
}
// return 0; This isn't needed - the code will never reach here.
}
It's also worth picking one coding formatting style and sticking to it rather than using a mixture as it makes for neater code.

Newbie syntax issue: error: expected ';' before ')' token

On both for loops (ie: for(len... and for(wid... ), I receive the same error message:
error: expected ';' before ')' token
void
init(void)
{
//fills board up with numbers
int tile = (d*d - 1);
int len = 0;
int wid = 0;
for(len < d; len++)
{
for(wid < d; wid++)
{
board[len][wid] = tile;
tile--;
}
}
}
Sorry to ask a similar question as before, but I'm a very confused Newbie!
Every for-loop needs to have its 3 parts (initialization, test, update) and if you don't have one or more of them, you still have to supply the two ;, so
for(len < d; len++)
^
|
should really be
|
v
for(;len < d; len++)
and the same for the other for-loop in your function.
For instance, this is how you would set up an infinite loop using for:
for(;;)
where all parts are skipped, but the two semi-colons are still required.
Perhaps this is tutorial/reference on the for-loop is helpful as a review/reference.
In general, a for loop has three parts:
for (initialization; check; update) { ... }
In your code, you are missing the initialization section.
You are missing a semi colon in your for loops to denote that you don't need an initialization clause (since you do it above):
void
init(void)
{
//fills board up with numbers
int tile = (d*d - 1);
int len = 0;
int wid = 0;
for(;len < d; len++)
{
for(;wid < d; wid++)
{
board[len][wid] = tile;
tile--;
}
}
}

What's wrong in this C program..?

struct bucket
{
int nStrings; //No. of Strings in a Bucket.
char strings[MAXSTRINGS][MAXWORDLENGTH]; // A bucket row can contain maximum 9 strings of max string length 10.
};//buck[TOTBUCKETS];
void lexSorting(char array[][10], int lenArray, int symb) //symb - symbol, sorting based on character symbols.
{
int i, j;
int bucketNo;
int tBuckNStrings;
bucket buck[TOTBUCKETS];
for(i=0; i<lenArray; i++)
{
bucketNo = array[i][symb] - 'a'; // Find Bucket No. in which the string is to be placed.
tBuckNStrings = buck[bucketNo].nStrings; // temp variable for storing nStrings var in bucket structure.
strcpy(buck[bucketNo].strings[tBuckNStrings],array[i]); // Store the string in its bucket.
buck[bucketNo].nStrings = ++tBuckNStrings; //Increment the nStrings value of the bucket.
}
// lexSorting(array, lenArray, ++symb);
printf("****** %d ******\n", symb);
for(i=0; i<TOTBUCKETS; i++)
{
printf("%c = ", i+'a');
for(j=0; j<buck[i].nStrings; j++)
printf("%s ",buck[i].strings[j]);
printf("\n");
}
}
int main()
{
char array[][10] = {"able","aback","a","abet","acid","yawn","yard","yarn","year","yoke"};
int lenArray = 10;
int i;
printf("Strings: ");
for(i=0; i<lenArray; i++)
printf("%s ",array[i]);
printf("\n");
lexSorting(array, lenArray, 0);
}
Well here is the complete code, that I am trying. since its been a long time since i have touched upon C programming, so somewhere i am making mistake in structure declaration.
The problem goes here:-
1) I have declared a structure above and its object as array(buck[]).
2) Now when I declare this object array along with the structure, it works fine.. I have commented this thing right now.
3) But when I declare this object array inside the function.. because ultimately i have to declare inside function( as i need to build a recursive program, where objects will be created in very recursive call) then the program is throwing segmentation fault.
Expected Output
> [others#centos htdocs]$ ./a.out
> Strings: able aback a abet acid yawn
> yard yarn year yoke
> ****** 0 ******
> a = able aback a abet acid
> b =
> c
> .
> .
> y = yawn yard yarnyear yoke
> z =
Actual Output
[others#centos htdocs]$ ./a.out
Strings: able aback a abet acid yawn yard yarn year yoke
Segmentation fault
I have no idea, what difference I made in this. Kindly help.
Thanks.
What's wrong with your program is that it doesn't contain a main() function hence it won't link.
Beyond that, you should always do the following when asking questions here:
Provide a complete, minimal code sample that demonstrates the problem.
Detail the expected behaviour.
Detail the actual behaviour.
In fact, when I add the line:
int main (void) { return 0; }
it compiles and links fine.
That means it's almost certainly a run-time error you're experiencing hence we need the main() to figure out what you're doing wrong.
Using my psychic debugging skills, an important difference between declaring it at file scope and block scope is that the file-scope version will be initialised to zeros.
That means all the structure fields will be effectively zero (for the count) and empty strings (for the strings). With block scope, those counts and strings will be uninitialised.
The fact that you're using TOBUCKETS to print the structure out probably means you're trying to print out one of those uninitialised strings.
I think what's probably happening is that the nStrings field contains a garbage value when you start the processing. You should probably initialise it to zero manually (with a loop) and see if that fixes your problem. Put this after the declaration of buck in your sort function:
for (i = 0; i < TOTBUCKETS; i++)
buck[i].nStrings = 0;
Right. It turns out that was the problem. When I fix up the errors in your latest code, I get the segmentation violation as well but, when I add that section above, it works fine:
#include <stdio.h>
#include <string.h>
#define MAXSTRINGS 9
#define MAXWORDLENGTH 10
#define TOTBUCKETS 26
struct bucket
{
int nStrings;
char strings[MAXSTRINGS][MAXWORDLENGTH];
};
void lexSorting(char array[][10], int lenArray, int symb)
{
int i, j;
int bucketNo;
int tBuckNStrings;
struct bucket buck[TOTBUCKETS];
for(i=0; i<TOTBUCKETS; i++) buck[i].nStrings = 0;
for(i=0; i<lenArray; i++)
{
bucketNo = array[i][symb] - 'a';
tBuckNStrings = buck[bucketNo].nStrings;
strcpy(buck[bucketNo].strings[tBuckNStrings],array[i]);
buck[bucketNo].nStrings = ++tBuckNStrings;
}
printf("****** %d ******\n", symb);
for(i=0; i<TOTBUCKETS; i++)
{
printf("%c = ", i+'a');
for(j=0; j<buck[i].nStrings; j++)
printf("%s ",buck[i].strings[j]);
printf("\n");
}
}
int main()
{
char array[][10] = {"able","aback","a","abet","acid",
"yawn","yard","yarn","year","yoke"};
int lenArray = 10;
int i;
printf("Strings: ");
for(i=0; i<lenArray; i++)
printf("%s ",array[i]);
printf("\n");
lexSorting(array, lenArray, 0);
}
The output of that was:
Strings: able aback a abet acid yawn yard yarn year yoke
****** 0 ******
a = able aback a abet acid
b =
c =
d =
e =
f =
g =
h =
i =
j =
k =
l =
m =
n =
o =
p =
q =
r =
s =
t =
u =
v =
w =
x =
y = yawn yard yarn year yoke
z =
keyword struct is not required when you create objects for it.

Resources